-1

我在三个谷歌表上运行了一个相同的脚本,它在其中两个上运行良好,但在一个上它不会生成,不断向我抛出以下错误:

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;
}

4

1 回答 1

1

因此,在下面的这一行中,所有元素都是null,这就是您收到TypeError: Cannot read property 'getSheetName' of undefined错误消息的原因。

for ([i, sheet] in sheets)

您可以像这样更改for循环:

for (var i = 0; i < sheets.length; i++) {
    var sheetName = sheets[i].getSheetName();
    Logger.log("Working on sheet: " + sheetName);

    if (ignoreThisSheets.indexOf(sheetName) >= 0) {
        Logger.log("Skipping sheet: " + sheetName);
        continue;
    }

    var currentSheetRange;
    // Get the range of the sheet that is your paste target.
    var pasteRange;
    Logger.log('masterSheet.getLastRow() is:' + masterSheet.getLastRow())
    Logger.log('masterSheet.getLastColumn() is:' + masterSheet.getLastColumn())
    Logger.log('sheets[i].getLastRow() is:' + sheets[i].getLastRow())
    Logger.log('sheets[i].getLastColumn() is:' + sheets[i].getLastColumn())


    //  currentSheetRange = getRangeFromSheetsToCopy(sheet, 1, 1);
    currentSheetRange = sheets[i].getRange(2, 1, getLastRow(sheets[i]), sheets[i].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
    });


}

此外,在 中Apps Script,如果您想要,您可以使用log,如果您检查 ,您应该会看到所有记录的值。valueLogger.log(value)Logs

于 2020-02-25T13:57:54.810 回答