简体   繁体   English

进行编辑后,如何将范围从一个Google表格复制到另一个表格?

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

相关问题 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells 如何将范围从一个工作表复制到另一工作表的范围,然后重复复制到下一行 - How to copy a range from one sheet to a range in another sheet, then repeat copying to the next row down 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 在我将一系列数据从一张纸复制到另一张纸后获取要添加的日期戳 - Getting a date stamp to add in after i copy a range of data from one sheet to another Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script 将一行从一个Google表格电子表格复制到表单提交中的另一表格 - Copy a row from one Google Sheet Spreadsheet to Another on Form Submit 如何使用 Google Sheets Macros 从一张工作表复制值并将它们粘贴到另一张工作表中? - How to copy values from one sheet and paste them into another using Google Sheets Macros?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM