简体   繁体   English

如何在Apps Script / Google表格中的最后一个单元格开始自动填充到相邻列的最后一行?

[英]How to start autofill at last cell in one column to last row of adjacent column in Apps Script/Google Sheets?

So I have a table that will have data to the last row except column B. (for example, cols A, C, D etc. stop at row 40, but B stops at maybe row 25).所以我有一个表,它将包含除 B 列之外的最后一行的数据。(例如,列 A、C、D 等在第 40 行停止,但 B 可能在第 25 行停止)。 I want to programmatically keep the series going starting at the last cell with data in B and autofill down to the last row in the spreadsheet.我想以编程方式保持系列从 B 中数据的最后一个单元格开始,并自动填充到电子表格中的最后一行。 (Hopefully a script doing this will recognize the series and not just copy the same data to all the empty cells. When I do it manually it works.) I have something started here but I can't figure out how to call out the range of where to start the series. (希望执行此操作的脚本能够识别该系列,而不仅仅是将相同的数据复制到所有空单元格。当我手动执行时,它可以工作。)我从这里开始了一些事情,但我不知道如何调出范围从哪里开始这个系列。 I get an error on line 7 "Exception: Range not found".我在第 7 行“异常:未找到范围”上收到错误消息。

谷歌表格的图片

 function fillDownFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var sc = ss.getRange("B1:B").getValues();
  var scr = sc.filter(String).length;
  var lr = ss.getLastRow();
  var fillDownRange = ss.getRange(scr,2,lr)
  ss.getRange(scr).copyTo(fillDownRange);
  
}

Here is one way you could have the last fill value copy down.这是一种可以将最后一个填充值复制下来的方法。 The function grabs all of the values, maps the row that needs to fill down and then copies that last value down the rest of the sheet.该函数获取所有值,映射需要填充的行,然后将最后一个值复制到工作表的其余部分。

function fillDownFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var tab = ss.getActiveSheet();
  var sc = tab.getDataRange().getValues();
  var lastRow = sc.length;
  var values = sc.map(function(r) { return r[1]; }).filter(i => i);
  var lastFillRow = values.length;
  var fillDownValue = values.slice(-1);
  tab.getRange(lastFillRow + 1, 2, lastRow - lastFillRow).setValue([fillDownValue]);
}

In addition to the answers already provided, Apps Script already has an autoFill() method that you can use to do this.除了已经提供的答案之外,Apps 脚本已经有一个autoFill()方法,您可以使用它来执行此操作。 With this you just have to define a source and a destination range.有了这个,你只需要定义一个源和一个目标范围。 This works the same as selecting the range and dragging down your mouse manually on the corner.这与选择范围并在角上手动向下拖动鼠标相同。

function fillDownFunction() {
  var ss= SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
  var lastsheetrow = ss.getLastRow() //last row with data
  
  //last row in the autofill range with data
  var lastrangerow = ss.getRange("B1:B").getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow()
  
  var sourcerange = ss.getRange(2, 2,lastrangerow-1) //in your screenshot, this would be B2:B6
  var destinationrange = ss.getRange(2, 2, lastsheetrow-1) //This would be B2:B12
  
  sourcerange.autoFill(destinationrange, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES)

}

Note that I skipped the header row and offset the range lengths by -1 to compensate.请注意,我跳过了标题行并将范围长度偏移了 -1 以进行补偿。 This is because the autofill logic uses the entire range, so it would also take into account "Item2" and repeat it every 6 rows as "Item3", "Item4" and so on.这是因为自动填充逻辑使用整个范围,因此它还会考虑“Item2”并每 6 行重复一次,如“Item3”、“Item4”等。 A disadvantage in this case, but may prove useful if you plan to use autofill in more complex ways in the future.在这种情况下是一个缺点,但如果您计划在未来以更复杂的方式使用自动填充,可能会被证明是有用的。

暂无
暂无

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

相关问题 Google Sheets Apps Scripts如何在另一个指定列的最后一个非空单元格的同一行中填充特定文本 - Google Sheets Apps Scripts how to populate specific text in the same row of the last non-empty cell in another specified column 谷歌表脚本获取列中最后一个条目的行索引 - google sheets script Get row index of last entry in a column Google Apps脚本-在电子表格中查找列的最后一个空行 - Google Apps Script - Find Last Empty Row of a Column in Spreadsheet Google Apps 脚本 - 如果表中的最后一列,则使用新行粘贴数据 - Google Apps Script - If Last Column in table, then paste data with new row 如何在不考虑使用应用程序脚本(谷歌电子表格)的第一行(标题)的情况下获取列的最后一行值? - how to get last row value of a column without considering the first row(header) using apps script(google spreadsheet)? 适用于Excel脚本的Google脚本等效于行的最后一列偏移一 - Google script equivalent for Excel script to last column in row offset by one Google表格:如何找到每列的最后一行? - Google Sheets: How to find the last row of each column? 您如何在 Google 表格的 A 列中找到最后使用的行 - How do you find the last used row in column A of Google Sheets 如何使用谷歌应用程序脚本找到 A 列中的最后一行并打印时间戳? - How to find the last row in column A and print the timestamp by using google apps script? 如何选择与另一个单元格在同一行中的一个列中的一个单元格,该单元格正在使用 Google Apps 脚本在另一列中进行编辑? - How to select a cell in one column in the same row that the other cell, that is being edited in the other column using Google Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM