![](/img/trans.png)
[英]How can copy specifics sheet to another spreadsheet using google script
[英]How to copy spreadsheet's sheet into another spreadsheet without creating a new sheet copy when using google sheet script?
1.我有2个电子表格。 我们称它们为 A 和 B。
2.A有一张纸,我们称它为X,而B也有一张纸,我们称其为Y。
3.X 包含我想复制到 Y 中的所有信息,例如值、格式、图片(我放入单元格中)
4.因为X里面有图片。所以我不能使用importrange来获取Y中的图片,因为它们在不同的传播中,所以我必须使用脚本将它复制到Y中。
5.我的目标只是将X复制到Y而不在B中创建其他工作表。
5.起初,我使用这样的代码
function CopySheet() {
var ss = SpreadsheetApp.openById("A's ID"); //Get spread A
var cs = SpreadsheetApp.getActiveSpreadsheet(); //Get spread B
var sourceSheet = ss.getSheetByName("X's name"); //Get sheet X
sourceSheet.copyTo(cs); //Copy X into B (create a new sheet Z)
}
虽然新创建的工作表 Z 可以显示图片,但在 Spread BI 中还有其他工作表使用将引用保留为 Y 的公式。如果我删除 Y 并将 Z 重命名为 YI,我发现每个公式的引用都丢失了!
那么,脚本是否有某种方法只能将 X 复制到当前存在的 Y 中而不创建新表?
注意:此解决方案是由@Cooper 提供给您的,他在此问题的评论中建议了可能的答案。 此答案作为社区维基发布。
为了实现您的目标,这对我有用:
X
从A
复制到B
B
Copy of X
副本中的所有内容Copy of X
到Y
Copy of X
以下代码是如何工作的示例。 您可以在代码的注释中找到对它的解释:
function myFunction() { // get spreadsheets A and B var a = SpreadsheetApp.openById('ID SHEET A'); var b = SpreadsheetApp.openById('ID SHEET B'); // copy sheet X into spreadhseet B a.getSheetByName('X').copyTo(b); // copy the values of the cells in the sheet just created in spreadsheet B var range = b.getSheetByName('Copy of X').getRange(b.getSheetByName('Copy of X').getLastRow(), b.getSheetByName('Copy of X').getLastColumn()); // paste all the content of the sheet X into Y (into the desired range of Y) range.copyTo(b.getSheetByName('Y').getRange('A1')); // delete the sheet that was created when passing the information from one spreadsheet to another b.deleteSheet(b.getSheetByName('Copy of X')); }
我希望这对你有帮助。 如果您需要其他任何东西或者您不理解某些东西,请告诉我。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.