简体   繁体   English

应用程序脚本将多个范围从一张工作表复制到另一个电子表格

[英]Apps script copy multiple range from 1 sheet to another spreadsheet

I am trying to copy data from 1 spreadsheet to another, I have successfully implemented something i found online that works with a specific range我正在尝试将数据从一个电子表格复制到另一个电子表格,我已经成功实现了我在网上找到的适用于特定范围的东西

function cloneGoogleSheet() {

  // source doc
  var sss = SpreadsheetApp.openById("spreadsheetkey1");

  // source sheet
  var ss = sss.getSheetByName('_tab_name_source');

  // Get full range of data
  var SRange = ss.getRange(7,3,5,1);

  // get A1 notation identifying the range
  var A1Range = SRange.getA1Notation();

  // get the data values in range
  var SData = SRange.getValues();

  // target spreadsheet
  var tss = SpreadsheetApp.openById("spreadsheetkey2");

  // target sheet
  var ts = tss.getSheetByName('tab_name_destination');

  // Clear the Google Sheet before copy
  //ts.clear({contentsOnly: true});

  // set the target range to the values of the source data
  ts.getRange(A1Range).setValues(SData);

};

The above piece coding work perfectly however I need to copy 18 different ranges that i cant just merge into 1 range.上面的代码工作完美,但是我需要复制 18 个不同的范围,我不能只合并到 1 个范围中。 I considered the option of using the above however "multiplying" it 18 times for each range that however seems like a very inelegant solution.我考虑过使用上面的选项,但是对于每个范围“乘”它 18 次,但这似乎是一个非常不雅的解决方案。

I found a working solution that works if it stays within the same spreadsheet since it uses copyto instead of get/set values.我找到了一个可行的解决方案,如果它保留在同一个电子表格中,因为它使用 copyto 而不是获取/设置值。 The values part works perfectly since it doesnt mess with merge cells formatting.值部分完美地工作,因为它不会与合并单元格格式混淆。 I have been struggling past 2-3 hours in merging the below-working code with elements from the first code to make a working script.在将以下工作代码与第一个代码中的元素合并以制作工作脚本方面,我一直在苦苦挣扎 2-3 个小时。

function test () {
  try {
    var spread = SpreadsheetApp.openById("spreadsheetkey");
    var sheet = spread.getSheetByName("tab_name_source");
    var rlist = sheet.getRangeList(["c7:c11", "g7:g11", "k7:k11"]);
    sheet = spread.getSheetByName("tab_name_destination");
    for( var i=0; i<rlist.getRanges().length; i++ ) {
      var r1 = rlist.getRanges()[i];
      var r2 = sheet.getRange(r1.getA1Notation());
     
      r1.copyto(r2);
    }
  }
  catch(err) {
    Logger.log(err);
  }
}

I tried initially to adapt the 2nd piece of coding to using setvalues however i had not been able to succesfully implement the part of getvalues within the scope of this code.我最初尝试将第二段编码调整为使用 setvalues,但是我无法在此代码的 scope 中成功实现 getvalues 部分。 I figured once I got this piece of code working with get and set values instead of Copyto i would only need to add the spreadsheetid of the other spreadsheet to get the final result我想一旦我得到这段代码使用 get 和 set values 而不是 Copyto,我只需要添加另一个电子表格的电子表格 ID 即可获得最终结果

Try this:尝试这个:

function myFunction() {
  var sourceSS = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = sourceSS.getSheetByName("sheetname");
  var targetSS = SpreadsheetApp.openById("spreadsheet id here");
  var targetSheet = targetSS.getSheetByName("Sheet1");

  var ranges = ["C7:C11", "G7:G11", "K7:K11"];
  ranges.forEach(range => {
    var data = sourceSheet.getRange(range).getValues();
    targetSheet.getRange(range).setValues(data);
  })
}

Source sheet:源表:

在此处输入图像描述

Destination sheet:目的地表:

在此处输入图像描述

References:参考:

暂无
暂无

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

相关问题 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] Apps脚本复制工作表到另一个电子表格中非常大的数据 - Apps Script Copy sheet to another spreadsheet very large data 如何使用 Apps 脚本从具有条件和特定范围的另一个电子表格中复制和粘贴数据 - How to copy and paste data from another spreadsheet with condition and specific range with Apps Script 将值从一个范围复制到另一个电子表格末尾的脚本 - Script to copy values from a range to the end of another spreadsheet 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? Google Apps 脚本,将一个电子表格复制到另一个带格式的电子表格 - Google Apps Script, copy one spreadsheet to another spreadsheet with formatting 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script Google Script:将值从一张纸复制到另一张纸以确定范围 - Google Script: copy values from one sheet to another determining the range 从模板电子表格复制Google Apps脚本 - Copy Google Apps Script from template spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM