[英]Google Sheets Apps Script Matching Source Destination Sheets having same name tabs
我需要在源和目标表的选项卡之间复制值。 两个工作表中的选项卡名称相同为 H1、H2、H3。 复制在同名选项卡之间进行,即 H1->H1、H2->H2 等。脚本附在下面。 每次我必须运行脚本时,我都会手动更改工作表名称。 如何在一个 go 中做到这一点?
function updateSourceToTarget(sourceID,sourceName,targetID,targetname){
var source = SpreadsheetApp.openById("Source").getSheetByName("H1");
var destination = SpreadsheetApp.openById("Dest").getSheetByName("H1");
var sourcelastRow = source.getLastRow();
var sourcelastCol = source.getLastColumn();
var sourcedata = source.getRange(1,1,sourcelastRow,sourcelastCol).getValues();
destination.getRange(1,1,sourcelastRow,sourcelastCol).setValues(sourcedata);
}
使用forEach循环遍历工作表名称数组["H1","H2","H3"]
。
function updateSourceToTarget(sourceID,sourceName,targetID,targetname){
const sheetNames = ["H1","H2","H3"];
sheetNames.forEach(h=>{
let source = SpreadsheetApp.openById("Source").getSheetByName(h);
let destination = SpreadsheetApp.openById("Dest").getSheetByName(h);
let sourcelastRow = source.getLastRow();
let sourcelastCol = source.getLastColumn();
let sourcedata = source.getRange(1,1,sourcelastRow,sourcelastCol).getValues();
destination.getRange(1,1,sourcelastRow,sourcelastCol).setValues(sourcedata);
});
}
我猜这是使用 function 参数的最终版本:
function updateSourceToTarget(sourceID,targetID){
const sheetNames = ["H1","H2","H3"];
sheetNames.forEach(h=>{
let source = SpreadsheetApp.openById(sourceID).getSheetByName(h);
let destination = SpreadsheetApp.openById(targetID).getSheetByName(h);
let sourcelastRow = source.getLastRow();
let sourcelastCol = source.getLastColumn();
let sourcedata = source.getRange(1,1,sourcelastRow,sourcelastCol).getValues();
destination.getRange(1,1,sourcelastRow,sourcelastCol).setValues(sourcedata);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.