[英]How do you copy cells from one spreadsheet to a sheet in a different spreadsheet?
I'm trying to create a script that allows a user to copy a highlighted range of cells to another, unopened, google sheet document. 我正在尝试创建一个脚本,允许用户将突出显示的单元格范围复制到另一个未打开的Google工作表文档中。 I can make it work if the sheet I'm copying data to is in the current document, but I can NOT figure out how to make the data copy to a different spreadsheet.
如果我正在复制数据的工作表在当前文档中,我可以使它工作,但我无法弄清楚如何将数据复制到不同的电子表格。
I've tried openById or openbyUrl , but I keep getting the following error: 我试过openById或openbyUrl ,但是我一直收到以下错误:
"Target range and source range must be on the same spreadsheet." “目标范围和来源范围必须位于同一电子表格中。”
function copyToDifferentDocument() { var ss = SpreadsheetApp.getActiveSpreadsheet(); // *** Have to figure out how to make the target a different document!!!! *** var target = SpreadsheetApp.openById("targetsheetIDgoeshere"); /* Next we need to pick the particular sheets within those spreadsheets. Let's say your row is on the sheet named "New Stuff", and you have a sheet in the target spreadsheet named "Archive". */ var source_sheet = ss.getSheetByName("New Stuff"); var target_sheet = target.getSheetByName("Archive"); // The below makes the highlighted cells the range that will be copied. var source_range = source_sheet.getActiveRange(); var last_row = target_sheet.getLastRow(); target_sheet.insertRowAfter(last_row); var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1)); // Take the highlighted rant and put it on the last row of the target sheet. source_range.copyTo(target_range); }
I'm trying to get the highlighted range to copy to a sheet in a different google sheet document. 我正在尝试将突出显示的范围复制到不同的Google工作表文档中的工作表。
If my understanding is correct, how about this modification? 如果我的理解是正确的,那么这个修改怎么样? I think that there are 2 patterns for your situation.
我认为你的情况有两种模式。
In this pattern, only values are copied. 在此模式中,仅复制值。 Please modify as follows.
请修改如下。 If you are not required to copy the format, you can use this.
如果您不需要复制格式,则可以使用此格式。
target_sheet.insertRowAfter(last_row); var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1)); // Take the highlighted rant and put it on the last row of the target sheet. source_range.copyTo(target_range);
To:
if (last_row > 0) target_sheet.insertRowAfter(last_row);
var target_range = target_sheet.getRange(last_row + 1, 1);
var copiedsheet = source_sheet.copyTo(target);
copiedsheet.getRange(source_range.getA1Notation()).copyTo(target_range);
target.deleteSheet(copiedsheet);
In this pattern, both values and formats are copied. 在此模式中,将复制值和格式。 Please modify as follows.
请修改如下。 Unfortunately,
copyTo()
can copy the range to the range on only the same Spreadsheet. 不幸的是,
copyTo()
只能将范围复制到同一个电子表格上的范围内。 This situation can be also seen at the error message of your question. 您可以在问题的错误消息中看到这种情况。 So in this modification, at first, the sheet is copied to the target Spreadsheet.
因此,在此修改中,首先将工作表复制到目标电子表格。 Then, the range is copied.
然后,复制范围。
target_sheet.insertRowAfter(last_row); var target_range = target_sheet.getRange("A"+(last_row+1)+":G"+(last_row+1)); // Take the highlighted rant and put it on the last row of the target sheet. source_range.copyTo(target_range);
To:
if (last_row > 0) target_sheet.insertRowAfter(last_row); var target_range = target_sheet.getRange(last_row + 1, 1); var copiedsheet = source_sheet.copyTo(target); copiedsheet.getRange(source_range.getA1Notation()).copyTo(target_range); target.deleteSheet(copiedsheet);
If I misunderstood your question, I apologize. 如果我误解了你的问题,我道歉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.