简体   繁体   English

使用谷歌工作表脚本时,如何将电子表格的工作表复制到另一个电子表格中而不创建新的工作表副本?

[英]How to copy spreadsheet's sheet into another spreadsheet without creating a new sheet copy when using google sheet script?

1.I have 2 spreadsheets. 1.我有2个电子表格。 let's call them A and B.我们称它们为 A 和 B。

2.A has a sheet,let's call it X while B also has a sheet,let's call it Y. 2.A有一张纸,我们称它为X,而B也有一张纸,我们称其为Y。

3.X has every information that I want to copy into Y,such as values,formats,pictures(that I put into the cell) 3.X 包含我想复制到 Y 中的所有信息,例如值、格式、图片(我放入单元格中)

4.Because X has the pics in it.So I cant use importrange to get the pics shows in Y since they were in different spread,so I have to copy it into Y using script. 4.因为X里面有图片。所以我不能使用importrange来获取Y中的图片,因为它们在不同的传播中,所以我必须使用脚本将它复制到Y中。

5.My goal is just copy X into Y without create other sheet in B. 5.我的目标只是将X复制到Y而不在B中创建其他工作表。

5.At first,I am using code like this 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)
 }

Although the newly created sheet Z can shows the pics, but in Spread BI have other sheet that use formulas that keeps refs with Y.If I delete Y and rename Z to YI found that every formula's refs are lost!虽然新创建的工作表 Z 可以显示图片,但在 Spread BI 中还有其他工作表使用将引用保留为 Y 的公式。如果我删除 Y 并将 Z 重命名为 YI,我发现每个公式的引用都丢失了!

So is there has some way with the scripits that can only copy X into currently existed Y without create new sheet?那么,脚本是否有某种方法只能将 X 复制到当前存在的 Y 中而不创建新表?

Solution解决方案

NOTE: This solution is brought to you by @Cooper who suggested the possible answer in a comment of this question.注意:此解决方案是由@Cooper 提供给您的,他在此问题的评论中建议了可能的答案。 This answer is posted as a community wiki.此答案作为社区维基发布。

To achieve what you aim, this is what worked for me:为了实现您的目标,这对我有用:

  1. Copy your sheet X from A to B将您的工作表XA复制到B
  2. Copy all the content from Copy of X in B to YB Copy of X副本中的所有内容Copy of XY
  3. Delete Copy of X删除Copy of X

The following code is an example of how this would work.以下代码是如何工作的示例。 You can find explanations to it in the comments of the code:您可以在代码的注释中找到对它的解释:

 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')); }

I hope this has helped you.我希望这对你有帮助。 Let me know if you need anything else or if you did not understood something.如果您需要其他任何东西或者您不理解某些东西,请告诉我。 :) :)

暂无
暂无

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

相关问题 如何使用Google脚本将详细信息表复制到另一个电子表格 - How can copy specifics sheet to another spreadsheet using google script 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] Google Sheet(脚本)将Sheet复制到另一个SpreadSheet并在目标SS中命名新工作表 - Google Sheet (Script) copy Sheet to another SpreadSheet and name new sheet in target SS 将工作表复制为新电子表格的脚本无法正常工作 - Script to copy sheet as a new spreadsheet not working well Google脚本可将电子表格中的工作表复制到新电子表格,并以特定单元格命名新电子表格 - Google script to copy sheet in spreadsheet to new spreadsheet and name new spreadsheet after specific cell Google 工作表:将工作表复制到其他电子表格时出错 - Google sheet: Error on copy sheet to other spreadsheet 使用 Python 使用 Google API 将电子表格工作表复制到另一个工作表 - Copy spreadsheet sheet to a another one with Google API using Python 将 Google 工作表值复制到另一个 Google 电子表格并使用按钮创建新工作表 - Copy Google Sheet Values to another Google Spreadsheet and create new sheet with a button 电子表格上的工作表副本 - copy of sheet on a Spreadsheet Google Spreadsheet-如何将单元格数据从一张工作表复制到另一张工作表中的单元格? - Google Spreadsheet - How to copy cell data from one sheet to a cell in another sheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM