Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google Sheets通过插入行向下复制公式_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script Google Sheets通过插入行向下复制公式

Google apps script Google Sheets通过插入行向下复制公式,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在从Excel和VBA过渡到GoogleSheets,不知道JavaScript 我看到,当我在数据集中的任何位置插入新行时,有一个对某些VBA的常见请求,它将从上面的行中复制公式。我知道如何添加屁股,我使用宏录制器录制了一行的插入,但录制器只录制了我选择在上面插入的特定行 我想让脚本抓取activecell的行号,以便它在光标所在的位置上是动态的 这是我到目前为止的代码。有人能帮我纠正一下吗 function test() { var spreadsheet = Spreadsheet

我正在从Excel和VBA过渡到GoogleSheets,不知道JavaScript

我看到,当我在数据集中的任何位置插入新行时,有一个对某些VBA的常见请求,它将从上面的行中复制公式。我知道如何添加屁股,我使用宏录制器录制了一行的插入,但录制器只录制了我选择在上面插入的特定行

我想让脚本抓取activecell的行号,以便它在光标所在的位置上是动态的

这是我到目前为止的代码。有人能帮我纠正一下吗

function test() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getActiveCell().getRow().activate();
  spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveCell().getRow(), 1);
  spreadsheet.getActiveCell().offset(0, 0, 1, spreadsheet.getActiveCell().getNumColumns()).activate();
};
解决方案: 您可以通过创建一个项目来实现您的目标

按照以下说明进行设置。设置完成后,您不必手动执行
myFunction
。每次在活动工作表中插入新行时,都会自动执行该命令

function myFunction(e) {
 const sh = SpreadsheetApp.getActiveSheet();
 if(e.changeType === 'INSERT_ROW') {
 const row = sh.getActiveRange().getRow();
 const formulas = sh.getRange(row-1,1,1,sh.getLastColumn()).getFormulasR1C1();
 sh.getRange(row,1,1,sh.getLastColumn()).setFormulasR1C1(formulas);  
  }
}

说明:
  • 转到使用上述代码,将其复制/粘贴到脚本编辑器中,然后单击保存

  • 单击当前项目的触发器:

  • 单击页面右下角的添加触发器

  • 然后准确地使用这些设置:


  • 谢谢你,马里奥斯!它工作得很好,只是在向下复制公式时,没有将行引用更改为新插入的行。我需要做什么改变才能做到这一点?另外,关于我可以在哪里自学谷歌表单的javascript,你有什么建议吗?@Hilly1请尝试我最新的答案。您可以查看官方文档,也可以在线查看教程。此外,当我选择一行时,是否可以通过(a)“在上面插入一行”或(b)“在下面插入一行,或两者兼而有之”的左键单击选项执行此操作?@Hilly1仅在下面执行,但我可以通过两种方式执行此操作。请接受答案,如果它帮助了你,并考虑投票你做了什么改变?我看不到事件触发器参数有任何更改。