【问题标题】:How do I add FOR LOOP to this Google Script code?如何将 FOR LOOP 添加到此 Google 脚本代码中?
【发布时间】:2021-10-18 02:48:08
【问题描述】:

此脚本将 SHEET1 中的数据块复制并粘贴到 SHEET2 中的最后一行 + 1。

我想添加一个循环,以便对于 SHEET 1 中的每一行数据,我可以在 SHEET2 中指定目标行,而不是块粘贴到最后一行 + 1。

例如,我在 SHEET1 的 RANGE A1:E6 中有 5 行数据。

  • 第 1 列:名称
  • 第 2 列:案例#
  • 第 3 栏:说明
  • 第 4 列:日期
  • 第 5 列:值
  • 第 6 列:Sheet2 中的目标行

因此,如果目标行指定为 50,则该行将覆盖 Sheet2 中第 50 行的数据。

下一行数据指定目标第 73 行,以便将数据行存放在 Sheet2 中的第 73 行等...

function dataTransfer1() {
  var ss = SpreadsheetApp.getActive(); //source sheet/this sheet
  var tss = SpreadsheetApp.openById("oweoeIE8373JDJudjd8383Iwejfi83"); //destination sheet ID

  var sh = ss.getSheetByName("ADD MISS 7") //name of subsheet w/in source sheet
  var tsh = tss.getSheetByName("JR") //name of subsheet w/in destination sheet
  var rowDestination = tsh.getLastRow()+1;
  var numRows = ss.getRange("A1").getValues();

  var copyRange = sh.getRange(2,14,numRows,5).getValues(); //source range
  var dumpRange = tsh.getRange(rowDestination,1,numRows,5); //destination range
   
 
  
  dumpRange.setValues(copyRange);




}

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:
    function copy2() {
      const ss = SpreadsheetApp.getActive();
      const sh1 = ss.getSheetByName('Sheet1');
      const sh2 = ss.getSheetByName('Sheet2');
      const vs1 = sh1.getRange(1, 1, 6, 6).getValues();
      vs1.forEach(r => sh2.getRange(r[5], 1, 1, 5).setValues([r.slice(0, 5)]));
    }
    

    【讨论】:

      【解决方案2】:
      function dataTransfer1() {
        var ss = SpreadsheetApp.getActive(); //source sheet/this sheet
        var tss = SpreadsheetApp.openById("oweoeIE8373JDJudjd8383Iwejfi83"); //destination sheet ID
      
        var sh = ss.getSheetByName("ADD MISS 7") //name of subsheet w/in source sheet
        var tsh = tss.getSheetByName("JR") //name of subsheet w/in destination sheet
      
        var values = sh.getDataRange().getValues();
        var trange = tsh.getRange(1, 14, tsh.getLastRow(), 5);
        var tvalues = trange.getValues();
      
        for (const value of values) {
          const i = value[5] - 1;
          tvalues[i] = value.slice(0, -1);
        }
        trange.setValues(tvalues);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        • 1970-01-01
        • 2021-08-04
        • 2020-01-31
        • 1970-01-01
        相关资源
        最近更新 更多