当源工作表添加了行时,如何编写在一个工作表中添加行的脚本

How to script the addition of a row in one sheet when the source sheet has a row added?

本文关键字:工作 一个 添加行 脚本 何编写 添加      更新时间:2023-09-26

我有一个谷歌表单,它填充了一个谷歌电子表格(源表)。我找到并自定义了一个脚本,将某些列(全部)从源工作表拉到新的选项卡/工作表中。显然,两张表中的行数必须相同,脚本才能正常运行。如果没有,则返回错误。每次提交新表单时,都会在源工作表中添加一行,从而使两个工作表不同步并破坏脚本。

我想帮助弄清楚我需要向现有脚本添加什么功能(或者我可以对它进行什么更改),这样当源工作表中出现新行时(因为表单已经提交),目标工作表中就会出现一个空行。

我需要调整我的脚本还是添加一个新函数?

function importFunction(a) {
  var ss = SpreadsheetApp.openById("0ApTaY3v27-UqdElwZTBvanNpaC1UckpxaTJRZS1XNWc");
  var sourceSheet = ss.getSheets()[0];
  var ss2 = SpreadsheetApp.openById("0ApTaY3v27-UqdElwZTBvanNpaC1UckpxaTJRZS1XNWc");
  var targetSheet = ss2.getSheets()[1];
  var values1 = sourceSheet.setActiveSelection("C:C").getValues();
  var values2 = sourceSheet.setActiveSelection("AD:AD").getValues();
  var values3 = sourceSheet.setActiveSelection("D:E").getValues();
  var values4 = sourceSheet.setActiveSelection("AE:AE").getValues();
  var values5 = sourceSheet.setActiveSelection("F:H").getValues();
  var values6 = sourceSheet.setActiveSelection("N:U").getValues();
  targetSheet.setActiveSelection("A:A").setValues(values1);
  targetSheet.setActiveSelection("B:B").setValues(values2);
  targetSheet.setActiveSelection("C:D").setValues(values3);
  targetSheet.setActiveSelection("E:E").setValues(values4);
  targetSheet.setActiveSelection("F:H").setValues(values5);
  targetSheet.setActiveSelection("I:P").setValues(values6);
}

根据下面的建议,我试图将脚本更改为以下内容,但我遇到了一个错误-找不到方法appendRow()。我该怎么解决?

    function importFunction(a) {
      var ss = SpreadsheetApp.openById("0ApTaY3v27-UqdElwZTBvanNpaC1UckpxaTJRZS1XNWc");
      var sourceSheet = ss.getSheets()[0];
      var ss2 = SpreadsheetApp.openById("0ApTaY3v27-UqdElwZTBvanNpaC1UckpxaTJRZS1XNWc");
      var targetSheet = ss2.getSheets()[1];
      var targetMax = targetSheet.getMaxRows();
      var values1 = sourceSheet.setActiveSelection("C:C").getValues();
      var values2 = sourceSheet.setActiveSelection("AD:AD").getValues();
      var values3 = sourceSheet.setActiveSelection("D:E").getValues();
      var values4 = sourceSheet.setActiveSelection("AE:AE").getValues();
      var values5 = sourceSheet.setActiveSelection("F:H").getValues();
      var values6 = sourceSheet.setActiveSelection("N:U").getValues();
      if(targetMax == values1.length) {
        targetSheet.setActiveSelection("A:A").setValues(values1);
        targetSheet.setActiveSelection("B:B").setValues(values2);
        targetSheet.setActiveSelection("C:D").setValues(values3);
        targetSheet.setActiveSelection("E:E").setValues(values4);
        targetSheet.setActiveSelection("F:H").setValues(values5);
        targetSheet.setActiveSelection("I:P").setValues(values6);
      }
      else
        targetSheet.appendRow();
    }

在使用getMaxRows()写入值之前,您可以简单地检查工作表SS2中的可用行数,并将其与数据数组value1.length的长度进行比较,然后,根据此比较,最终在for next循环中使用appendRow()添加所需的行。

EDIT:在您的EDIT之后,我进行了进一步的测试,appendRow()似乎没有像我想象的那样工作,它在空表的开头添加了Rows。。。所以我尝试了这个:

  if(targetMax < values1.length) {
  var RowsToAdd = values1.length-targetMax
  for(nn=0;nn<RowsToAdd;++nn){targetSheet.insertRowAfter(targetMax)}
  }

只需将其放置在var value6=... 之后

对这个小错误感到抱歉;-)

相关文章: