我在三个谷歌表上运行了一个相同的脚本,它在其中两个上运行良好,但在一个上它不会生成,不断向我抛出以下错误:
TypeError:无法读取未定义的属性“getSheetName”
这是脚本:
var masterSheetName = "GROUP 1";
var ignoreThisSheets=masterSheetName+ ",REFERENCE,";
var sortColumn=4;
var sortAscending = true;
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Custom Menu')
.addItem('Generate Master Sheet', 'menuItem1')
.addToUi();
}
function menuItem1() {
// insertFinalSheet();
collectDataFromSheets(masterSheetName);
}
function getRangeFromSheetsToCopy(thissheet, skipRows, skipCols) {
return thissheet.getRange(skipRows, skipCols, thissheet.getLastRow(), thissheet.getLastColumn());
}
function insertFinalSheet() {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var itt = activeSpreadsheet.getSheetByName(masterSheetName);
if (itt) {
activeSpreadsheet.deleteSheet(itt);
}
activeSpreadsheet.insertSheet(masterSheetName);
}
function collectDataFromSheets(masterSheetName) {
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = activeSpreadsheet.getSheets();
var masterSheet = activeSpreadsheet.getSheetByName(masterSheetName);
var range = masterSheet.getRange(2,1,masterSheet.getLastRow(),masterSheet.getLastColumn());
range.clear();
var colValues = [];
var first = true;
console.log("Sheets : " + sheets);
console.log("activeSpreadsheet : " + activeSpreadsheet);
for ([i, sheet] in sheets) {
console.log("i : " + i);
var sheetName = sheet.getSheetName();
console.log("Working on sheet: " +sheetName);
if(ignoreThisSheets.indexOf(sheetName)>= 0){
console.log("Skipping sheet: " +sheetName);
continue;
}
var currentSheetRange;
// Get the range of the sheet that is your paste target.
var pasteRange;
console.log('masterSheet.getLastRow() is:' + masterSheet.getLastRow())
console.log('masterSheet.getLastColumn() is:' + masterSheet.getLastColumn())
console.log('sheet.getLastRow() is:' + sheet.getLastRow())
console.log('sheet.getLastColumn() is:' + sheet.getLastColumn())
// currentSheetRange = getRangeFromSheetsToCopy(sheet, 1, 1);
currentSheetRange = sheet.getRange(2, 1, getLastRow(sheet), sheet.getLastColumn());
if(first)
pasteRange = masterSheet.getRange(2,1);
else
pasteRange = masterSheet.getRange(getLastRow(masterSheet)+1, 1);
var columnWidths = SpreadsheetApp.CopyPasteType.PASTE_NORMAL;
currentSheetRange.copyTo(pasteRange);
currentSheetRange.copyTo(pasteRange, columnWidths, false);
first = false;
masterSheet.getRange(2,1,masterSheet.getLastRow(), masterSheet.getLastColumn()).sort({column: sortColumn, ascending: sortAscending});
}
return colValues;
}
function getLastRow(sheet){
var Avals = sheet.getRange("A1:A").getValues();
console.log('Avals is:' + Avals)
var Alast = Avals.filter(String).length;
console.log('Alast is:' + Alast);
return Alast;
}