简体   繁体   English

谷歌工作表脚本自动添加带有复制公式的新行

[英]Google sheet script auto add new rows with copied formulas

I need to create function which adds rows with copied formulas from above rows.我需要创建 function ,它从上面的行中添加带有复制公式的行。 After the script is launched it should result in accurate number (set 5 i this code) of blank rows at the end of the sheet.脚本启动后,它应该会在工作表末尾产生准确的空白行数(在此代码中设置 5)。

The code I managed to create counts what number of rows should be added but adds only one row with copied formulas at the end.我设法创建的代码计算了应该添加的行数,但最后只添加了一行复制的公式。

Please help me edit this code to multiple the result of the function by "rowstoadd" parameter.请帮我编辑此代码,以通过“rowstoadd”参数将 function 的结果倍增。

Sheet image工作表图像

function autoaddRows() {

  var sheet = SpreadsheetApp.getActive().getSheetByName('Harmonogram');
  var range = sheet.getRange("B2:B").getValues();
  var lastRowB = range.filter(String).length + 2;
  var lastRowA = sheet.getLastRow();
  var blanknrows = sheet.getLastRow() - lastRowB;
  if (blanknrows < 5) {
    let rowstoadd = 5 - blanknrows;
    Browser.msgBox(rowstoadd);
    let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    let lastRowIndex = sheet.getLastRow();
    let existingRange = getRowRange(sheet, lastRowIndex);

    sheet.insertRowAfter(lastRowIndex);
    let newRange = getRowRange(sheet, ++lastRowIndex);

    existingRange.copyTo(newRange);
    newRange.setFormulas(newRange.getFormulas());
    newRange.clearNote();
  }
  
  function getRowRange(sheet, rowIndex) {
    return sheet.getRange(rowIndex, 1, 1, sheet.getLastColumn());
  }
}

I believe your goal is as follows.我相信你的目标如下。

  • You want to keep 5 new empty rows.您想保留 5 个新的空行。
  • For example, when the last row of column "A" is 10 and the last row of column "B" is 9, you want to add 4 new rows.例如,当“A”列的最后一行是 10 并且“B”列的最后一行是 9 时,您要添加 4 个新行。
  • Also, you want to put the formula to the column "B" of the inserted rows.此外,您希望将公式放入插入行的“B”列。

In this case, how about the following modified script?在这种情况下,下面的修改脚本怎么样?

Modified script:修改后的脚本:

function autoaddRows() {
  var addRows = 5;
  var sheet = SpreadsheetApp.getActive().getSheetByName('Harmonogram');
  var range = sheet.getRange("B2:B").getValues();
  var lastRowB = range.filter(String).length + 1;
  var lastRow = sheet.getLastRow();
  var blanknrows = lastRow - lastRowB;
  var diff = addRows - blanknrows;
  if (diff > 0) {
    sheet.insertRowsAfter(lastRow, diff);
    var range = sheet.getRange("B" + lastRowB);
    range.copyTo(range.offset(1, 0, diff + 1), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
    range.clearNote();
  }
}
  • In this modification, the difference between the last rows between column "A" and column "B" is retrieved.在此修改中,检索列“A”和列“B”之间的最后一行之间的差异。 And the empty rows are inserted using the difference.并且使用差异插入空行。
  • In your script, newRange.clearNote();在您的脚本中, newRange.clearNote(); is used.用来。 So, I also add range.clearNote();所以,我还添加range.clearNote(); . . If you want to remove it, please remove it.如果您想删除它,请删除它。

References:参考:

暂无
暂无

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

相关问题 Google Sheet Script - 在复制之前在列中添加日期 - Google Sheet Script - add date in colum before the copied 为什么在向电子表格添加公式后,我的工作表没有添加新的数据行? - Why doesn't my sheet add new rows of data, after adding formulas to the spreadsheet? 谷歌表格脚本将完成的行移动到新工作表 - Google Sheets Script to move completed rows to new sheet Google Sheet Auto Email 脚本 - Google Sheet Auto Email Script 如何制作谷歌工作表脚本忽略带有公式的字段 - How to make google sheet script Ignore fields with formulas 谷歌应用脚本:打开谷歌表格,等待所有公式处理完毕,关闭表格 - Google App Script: Open Google Sheet, wait for all formulas to process, close sheet 使用Google脚本复制页面后,电子表格公式不会更新。 我该如何解决这个问题 - Spreadsheet formulas are not updating after the pages are copied using Google script. How do I fix this issue Google Apps 脚本通过唯一 ID 查找和更新目标工作表中的行,如果唯一 ID 不在目标工作表中,则添加唯一行 - Google Apps Script to find and update rows in target sheet by unique ID and add unique rows if unique ID is not in the target sheet 获取 Google Sheet 脚本以跳过空行? - Get a Google Sheet script to skip empty rows? Google Sheet Script:如何在新专栏中自动更新Google Sheet&#39; - Google Sheet Script : How to autoupdate Google Sheet 'in new column'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM