![](/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.