简体   繁体   English

Google 工作表将整个工作表复制到另一个工作表

[英]Google sheets copy whole sheet to another

I have a google spreadsheet that includes a couple of sheets that are dynamically maintained from a variety of sources.我有一个谷歌电子表格,其中包括几张从各种来源动态维护的表格。 Call them Dynamic_data1 and Dynamic_data2.称它们为 Dynamic_data1 和 Dynamic_data2。

For each of these dynamic sheets there is a companion static sheet.对于这些动态表中的每一个,都有一个配套的 static 表。 Let's call them Static_data1 and Static_data2.我们称它们为 Static_data1 和 Static_data2。

I have a periodic need do the following for these two pairs of sheets, which I have been doing manually:我定期需要对这两对工作表执行以下操作,我一直在手动执行:

  1. Delete the entire contents of Static_data1删除Static_data1的全部内容
  2. Copy the entire contents of Dynamic_data1复制 Dynamic_data1 的全部内容
  3. "paste special/paste values only" the values from Dynamic_data1 into Static_data1 “仅粘贴特殊/粘贴值”将 Dynamic_data1 中的值放入 Static_data1

Then repeat the process for Dynamic_data2/Static_data2然后对 Dynamic_data2/Static_data2 重复该过程

It's always the whole sheet, regardless of the number of rows or columns present.无论存在多少行或列,它始终是整个工作表。

Doing this manually works fine, but I find I have the need to automate it.手动执行此操作很好,但我发现我需要自动化它。 I've done a little scripting, not much, so I am no expert.我做了一点脚本,不多,所以我不是专家。

I can't help but think this should be a simple job for a script.我不禁认为这应该是一个简单的脚本工作。 However, my inept googling has not located something I can adapt.但是,我无能的谷歌搜索没有找到我可以适应的东西。 I'd be grateful for advice.我会很感激你的建议。

Try something like this:尝试这样的事情:

function copyAll() {
  const ss=SpreadsheetApp.getActive();//Spreadsheet with spreadsheet id's
  const sh=ss.getSheetByName('FromToList');//sheet with spreadsheet id's
  const idA=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).getValues().map((r)=>{
    return {from:r[0],to:r[1]};//FromToList is a sheet with from id's in columnA and to id's in columnB
  });//tested down to here the rest is up to you.  I recommend not unleashing this on real spreadsheets until after you have thoroughly tested it.
  idA.forEach(obj=>{
    let fss=SpreadsheetApp.openById(obj.from);//from Spreadsheet
    let fshts=fss.getSheets();//from sheets
    let tss=SpreadsheetApp.openById(obj.to);//to Spreadsheet
    let tshts=tss.getSheets();//to sheets
    fshts.forEach(fsh=>{
      fsh.copyTo(tss);//copy all from sheets to to spreadsheet
    });
    tshts.forEach(tsh=>{
      tss.deleteSheet(tsh);//delete all original to sheets in to spreadsheet
    });
  });
}

Note: function has not be tested注:function 未经测试

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM