[英]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.