[英]How do I copy a range from one Google Sheet to another when edits are made?
I have a sheet that will update based on new articles in an RSS feed using importfeed() and the URLs are analyzed with importxml(). 我有一张工作表,该工作表将根据使用importfeed()的RSS feed中的新文章进行更新,并使用importxml()分析URL。 Problem is the data is always changing and I would like to archive old data from the importfeed() function.
问题是数据总是在变化,我想从importfeed()函数中存档旧数据。 I'd just like to move the most updated data from one sheet to another without overwriting anything.
我只想将最新数据从一张纸移动到另一张纸,而不会覆盖任何内容。
Here is the sample code from: https://stackoverflow.com/users/650206/jeremy : 以下是示例代码,来自: https : //stackoverflow.com/users/650206/jeremy :
function myFunction() {
// Get Spreadsheets
var source = SpreadsheetApp.openById("spreadsheetKeySource");
var target = SpreadsheetApp.openById("spreadsheetKeyTarget");
// Set Sheets
var source_sheet = source.getSheetByName("Sheet1");
var target_sheet = target.getSheetByName("Sheet1");
// Get target last row
var last_row = target_sheet.getLastRow();
// Set Ranges
var source_range = source_sheet.getRange("A1:B1");
var target_range = target_sheet.getRange("A"+(last_row+1)+":B"+(last_row+1));
// Fetch values
var values = source_range.getValues();
// Save to spreadsheet
target_range.setValues(values);
}
Problem with the above code is it only selects one row when I need it to select an entire range (eg A1:G55) and paste that data below the old data in another sheet. 上面代码的问题是,当我需要它选择整个范围(例如A1:G55)并将其粘贴到另一张表中的旧数据下方时,它仅选择一行。
Any help would be much appreciated! 任何帮助将非常感激!
EDIT 编辑
The error I'm getting when I change: 更改时出现的错误:
var source_range = source_sheet.getRange("A1:B1");
var target_range = target_sheet.getRange("A"+(last_row+1)+":B"+(last_row+1));
to: 至:
var source_range = source_sheet.getRange("A1:G25");
var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1));
is "Incorrect range height, was 5 but should be 1 (line 21, file "Code")" 是“范围高度不正确,为5,但应为1(第21行,文件“代码”))
which is this piece of code: 这是这段代码:
target_range.setValues(values);
var source_range = source_sheet.getRange("A1:G25");
var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1));
The lines above attempt to copy 25 rows of data and paste that into one row. 上面的几行尝试复制25行数据并将其粘贴到一行中。 I'm surprised that the error message says "was 5 but should be 1".
错误消息显示“是5但应为1”使我感到惊讶。 I'd have expected it to say "was 25 but should be 1".
我曾期望它说“是25,但应为1”。
Try this instead: 尝试以下方法:
var source_range = source_sheet.getRange("A1:G25");
var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+25));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.