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