简体   繁体   English

如何将单元格中的单元格复制到不同电子表格中的工作表?

[英]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: 我试过openByIdopenbyUrl ,但是我一直收到以下错误:

"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工作表文档中的工作表。

  • You want to copy the active range in the sheet of "New Stuff" on the active Spreadsheet to the last row in the sheet of "Archive" on the Spreadsheet of "targetsheetIDgoeshere" 您想要将活动电子表格中“新资料”工作表中的有效范围复制到“targetsheetIDgoeshere”电子表格上“存档”工作表中的最后一行

If my understanding is correct, how about this modification? 如果我的理解是正确的,那么这个修改怎么样? I think that there are 2 patterns for your situation. 我认为你的情况有两种模式。

Pattern 1: 模式1:

In this pattern, only values are copied. 在此模式中,仅复制值。 Please modify as follows. 请修改如下。 If you are not required to copy the format, you can use this. 如果您不需要复制格式,则可以使用此格式。

From: 从:
 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);

Pattern 2: 模式2:

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. 然后,复制范围。

From: 从:
 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); 

References: 参考文献:

If I misunderstood your question, I apologize. 如果我误解了你的问题,我道歉。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过比较不同电子表格中的两个单元格从工作表 A 中获取唯一消息中的所有数据 - How can I get all data in a unique message, from a sheet A from comparing two cells in different spreadsheet Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet 将一行从一个Google表格电子表格复制到表单提交中的另一表格 - Copy a row from one Google Sheet Spreadsheet to Another on Form Submit 如何将文本从Google电子表格的多个单元格拉到网站中的每个不同的位置 <p> 标签? - How do I pull text from multiple cells in a google spreadsheet to website each in a different <p> tag? 将多个工作表从一个电子表格复制到另一个电子表格 - Copy several sheets from one spreadsheet to another spreadsheet 我可以将数据从一个电子表格复制到新的电子表格吗? - Can I copy data from one spreadsheet to a new spreadsheet? 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 如何在编辑时将数据从一个电子表格复制到另一个电子表格 - How to copy data from one spreadsheet to another on edit 在编辑时将数据从一个电子表格复制到另一个电子表格 - Copy data from one spreadsheet to another on edit 如何从一张工作表中获取Google Spreadsheet中的数据行,并预先填充另一张工作表 - How to take rows of data in Google Spreadsheet from one sheet, and pre-populate another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM