简体   繁体   English

Google Sheets/Apps Script Copy 公式到新行

[英]Google Sheets/Apps Script Copy Formula to new row

still fairly new to this.对此仍然相当陌生。 I have been able to trigger the creation of a new row on a separate tab using a checkbox.我已经能够使用复选框在单独的选项卡上触发新行的创建。 On this new tab, there is a column that has formulas in it.在这个新选项卡上,有一列包含公式。 When the new row is created (above the first one), I want the formula in the new row.创建新行时(在第一行之上),我想要新行中的公式。 Here is what I have so far:这是我到目前为止所拥有的:

  if(sheet.getSheetName() == CLIENTS) {
    var checkCell = ss.getActiveCell();
    var checked = checkCell.getValue();
    var clientNameCell = checkCell.offset(0,-8).getValue();
    var clientType = checkCell.offset(0,-7).getValue();
    var sessionChargeCell = checkCell.offset(0, -6).getValue();
    var groupChargeCell = checkCell.offset(0,-5).getValue();
    var softwareFee = checkCell.offset(0,-4).getValue();
    var billFee = checkCell.offset (0,-3).getValue();
    var targetSheet = ss.getSheetByName("Client Billing Projections");
    var lCol = targetSheet.getLastColumn();
    if (checkCell.getColumn() == NEWCLIENTCHECK && checked == true) {
      var target = targetSheet.getRange(2, 1, 1, lCol);
      var formulas = target.getFormulas();
      targetSheet.insertRowBefore(2);
      var newRow = targetSheet.getRange(2,1,2,lCol);
      newRow.setFormulas(formulas);
    }
  }

I guess where I'm stuck is after the line targetSheet.insertRowBefore(2);我想我被卡住的地方是在targetSheet.insertRowBefore(2); . . I think it has something to do with my use of getFormulas and setFormulas, but I really can't wrap my head around it.我认为这与我对 getFormulas 和 setFormulas 的使用有关,但我真的无法理解它。 There's no syntax error here.这里没有语法错误。 As you can see, I've collected values from my first sheet (CLIENTS);如您所见,我已经从我的第一张表 (CLIENTS) 中收集了值; I intend to copy it onto this other tab, but I have to get the formula copied up as well.我打算将它复制到另一个选项卡上,但我也必须复制公式。 Any help would be appreciated.任何帮助,将不胜感激。

Here is an example of the sheet There's a lot of working code in there already. 这是工作表的一个例子那里已经有很多工作代码了。 Most of what you'll be looking at here is likely between lines 222 - 238. Everything else works so far.您将在这里看到的大部分内容可能在第 222 - 238 行之间。到目前为止,其他一切都有效。

  • When the script is run, a new row is inserted to the row 2 in the sheet of Client Billing Projections .运行脚本时,会在Client Billing Projections表中的第 2 行中插入一个新行。
  • You want to put the formula to the column "N" at the inserted row.您想将公式放在插入行的“N”列中。

If my understanding is correct, how about this modification?如果我的理解是正确的,这个修改怎么样?

Modification points:改装要点:

  • In your script, the formulas are retrieved by var formulas = target.getFormulas() from var target = targetSheet.getRange(2, 1, 1, lCol);在您的脚本中,公式由var formulas = target.getFormulas()var target = targetSheet.getRange(2, 1, 1, lCol); . . This is the 2 dimensional array including one row.这是包括一行的二维数组。 And the retrieved formulas are put using newRow.setFormulas(formulas) with var newRow = targetSheet.getRange(2,1,2,lCol) .检索到的公式使用newRow.setFormulas(formulas)var newRow = targetSheet.getRange(2,1,2,lCol) In this case, formulas has 2 dimensional array including 2 rows.在这种情况下, formulas具有包含 2 行的二维数组。
    • I thought that the reason of your issue might be this.我认为您的问题的原因可能是这个。

Modified script:修改后的脚本:

When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。

From: 从:
 var newRow = targetSheet.getRange(2,1,2,lCol);
To: 到:
 var newRow = targetSheet.getRange(2,1,1,lCol);

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一行的公式复制到谷歌应用程序脚本中的多行 - Copy one row's formula to multiple rows in google apps script Google Apps 脚本/Google 表格 - 仅复制 IF - Google Apps Script/ Google Sheets - Only copy IF 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script Google Sheets Apps Script Copy Row of Data 如果日期与标签名称相同 - Google Sheets Apps Script Copy Row of Data If date same as tab name Google Apps Script Google Sheets Gmail 处理新邮件追加行数组 - Google Apps Script Google Sheets Gmail Processing New Emails Append Row Array 如何在 Google Apps 脚本中动态创建新工作表时 append 第一行数据 - How to append first row data while creating new sheets dynamically in Google Apps Script Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet 用于在 Google 表格中移动和时间戳行的 Google Apps 脚本 - Google Apps Script for moving and timestamping row in Google Sheets 如果单元格以特定文本开头,则删除行 - Google Sheets / Apps Script - Delete Row if cell starts with certain text - Google Sheets / Apps Script Google Apps 脚本表 PasteDataRequest 粘贴到列而不是行? - Google Apps Script Sheets PasteDataRequest paste to column instead of row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM