![](/img/trans.png)
[英]getValue() function not returning anything in Google Scripts for Sheets
[英]forEach function for multiple sheets in Google Scripts
我正在尝试运行一个脚本,将信息从多个不同的工作表复制到不同的文件。 我在使用forEach
function 时遇到问题。我得到的错误是:
错误
ReferenceError: sheets 未定义 copy @ Code.gs:9 copyInfo @ Code.gs:4
function copyInfo() {
var sheets = SpreadsheetApp.getActive().getSheets(); // get all sheets
sheets.forEach(copy)
}
function copy() {
var sheetName = sheets.getActiveSheet.getName();
if(sheetName.includes("*"))
var copySheet = SpreadsheetApp.getActiveSheet();
var sheetName=copySheet.getName();
var pasteSpreadsheet = SpreadsheetApp.openById("1NXmdq6yCKC6oiDAQNXcovj85697__rCeVJiIT_ou_gI");
//Create new sheet if one doesn't exist
var newSheet = pasteSpreadsheet.getSheetByName(sheetName);
if (newSheet != null)
pasteSpreadsheet.deleteSheet(newSheet);
newSheet = pasteSpreadsheet.insertSheet();
newSheet.setName(sheetName);
//Copy and paste data
// Clear the Google Sheet before copy
newSheet.clear({contentsOnly: true});
// get source range
var source = copySheet.getRange("B2:B");
var values = source.getValues();
// get destination range
var destination = newSheet.getRange(1,1,values.length,values[0].length);
// copy values to destination range
destination.setValues(values);
}
如果您创建一个 function 与 forEach 一起使用,则该 function 需要接受一个单独的项目作为参数。 在这种情况下,来自 SpreadsheetApp.getActive().getSheets() 的每个“工作表”都将作为第一个参数传递到您的副本 function - 但副本 function 不接受任何参数。
function copy(sheet) {
var sheetName = sheet.getName();
if(sheetName.includes("*"))
...
}
function copyInfo() {
const dontcopy = ["Sh1","Sh2",...];
var sheets = SpreadsheetApp.getActive().getSheets(); // get all sheets
sheets.filter(sh => !~dontcopy.indexOf(sh.getName()).forEach(s => {
s.activate();
copy();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.