[英]Copy data from sheet to the last row of another workbook
I want to copy columns A2:K
(or: all data on sheet apart from row 1) to a completely different workbook and paste in the last row every time (this way I can keep running the script and it won't overwrite any previous pasted data).我想将A2:K
列(或:工作表上除第 1 行之外的所有数据)复制到完全不同的工作簿并每次都粘贴到最后一行(这样我可以继续运行脚本并且它不会覆盖任何以前的粘贴数据)。
Here is my code:这是我的代码:
function dataLog() {
var ss = SpreadsheetApp.getActive();
var ss = ss.getSheetByName('Data Sheet');
var target = SpreadsheetApp.openById('xxxxxx');
var ss = target.getSheetByName("Copy Of DataInput");
var vs = ss.getRange('A2:K').getValues();
ss.getRange(lastRow + 1,1,vs.length,vs[0].length).setValues(vs);
}
I'm getting an error on line 6:我在第 6 行收到错误:
TypeError: Cannot read properties of null (reading 'getRange') dataLog @ Untitled.gs:6
Based on the error, maybe you're trying to access a sheet that doesn't exist and that's why it is returning null. Make sure the sheet names Data Sheet
or Copy Of DataInput
are spelled correctly and it does exist in your Spreadsheet.根据错误,也许您正在尝试访问不存在的工作表,这就是它返回 null 的原因。确保工作表名称Data Sheet
或Copy Of DataInput
拼写正确并且它确实存在于您的电子表格中。 Tested the script and made a couple of updates and it works.测试了脚本并进行了几次更新,它可以正常工作。 You can try the following:您可以尝试以下操作:
function dataLog() {
var ss = SpreadsheetApp.getActive();
var ss = ss.getSheetByName('Data Sheet');
var vs = ss.getRange('A2:K').getValues();
var target = SpreadsheetApp.openById('XXX');
var ss2 = target.getSheetByName('Copy Of DataInput');
ss2.getRange(ss2.getLastRow() + 1,1,vs.length,vs[0].length).setValues(vs);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.