Apps 脚本 - .getLastColumn() 在 for 循环中运行一次然后出错

问题描述 投票:0回答:1

我正在尝试循环访问 Sheets 和 getLastColumn()。我激活第一个工作表并获取最后一列,然后激活下一个工作表,但这次我在 getLastColumn() 上收到错误。我在 for 循环中执行此操作,这可能是问题所在吗?

    //sort fab, milling, turning, engraving
    sheet = SpreadsheetApp.getActive().getSheetByName("Fab Schedule");
    sheet.activate;
  
    var lastColumnFab = sheet.getLastColumn();
    var lastColumn = lastColumnFab;
    var lastColumnMilling;
    var lastColumnTurning;
    var lastColumnEngraving;
    var currentSheet = SpreadsheetApp.getActiveSheet().getSheetName(); 
    Logger.log(currentSheet);

    for(a=0;a<3;a++){ // 'a' is for looping through the different sheets
    Logger.log(a);

    // counting non-empty rows 
    var BValues = sheet.getRange("B3:B").getValues();
    var i=0;
    var singleBValue = "";
    //Logger.log(BValues[i]);
    for(i = 0; BValues[i] != ""; i++){
      singleBValue = BValues[i];
      //console.log({i,singleBValue});
    }
    var lastRow = i;
    i=i+3;

    // arrays begin with 0 // all rows and columns begin with 1
    sheet.getRange(3,2,lastRow,lastColumn).activate();
    //sheet.getRange('B3:V').activate(); //number of columns is different on every
    sheet
    var statusColumn = getHeaderNumber("Status")+1; //get header uses array, so it
    starts at 0
    var dueDateColumn = getHeaderNumber("Due Date")+1;
    var engineerColumn = getHeaderNumber("Engineer")+1;
  
    //sort by incomplete->complete, then
    sheet.getActiveRange().sort([{column: statusColumn, ascending: false}, {column: dueDateColumn, ascending: true},{column:engineerColumn, ascending: true}]);
  //sheet.getActiveRange().sort([{column: 14, ascending: false}]);//, {column: 18, ascending: true},{column:2, ascending: true}]);

  sheet.getRange(3,14,lastRow,lastColumn-14).setHorizontalAlignment("center"); //number of columns is different on every sheet
  sheet.getRange(3,1,lastRow,lastColumn).setVerticalAlignment("middle");
  sheet.getRange(3,1,lastRow,lastColumn).setFontFamily("Arial");
  sheet.getRange(3,1,lastRow,lastColumn).setFontSize(10);
  if (a==0){
      sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Milling Schedule");
      sheet.activate();
      var currentSheet = SpreadsheetApp.getActiveSheet().getSheetName(); 
      Logger.log(currentSheet);
      lastColumnMilling = sheet.getLastColumn(); //error?
      lastColumn = lastColumnMilling;
    }

Exception: The coordinates of the range are outside the dimensions of the sheet. Sortbyincompleteanddate @ sort.gs:62

google-apps-script
1个回答
0
投票

代码无法正常工作,因为

for (a
循环迭代四次,但代码仅尝试处理两张纸。使用更结构化的方法,也许像这样:

function test() {
  ['Fab Schedule', 'Milling Schedule', 'Turning Schedule', 'Engraving Schedule',]
    .forEach(sheetName => {
      const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
      if (!sheet) {
        console.log(`There is no sheet by the name ${sheetName}`);
        return;
      }
      sortAndFormat_(sheet);
    });
}

function sortAndFormat_(sheet) {
  const lastRow = sheet.getLastRow();
  const lastColumn = sheet.getLastColumn();
  sheet.getRange(3, 2, lastRow, lastColumn)
    .sort([
      { column: getHeaderNumber(sheet, 'Status') + 1, ascending: false },
      { column: getHeaderNumber(sheet, 'Due Date') + 1, ascending: true },
      { column: getHeaderNumber(sheet, 'Engineer') + 1, ascending: true },
    ]);
  sheet.getRange(3, 14, lastRow, lastColumn - 14).setHorizontalAlignment('center');
  sheet.getRange(3, 1, lastRow, lastColumn)
    .setVerticalAlignment('middle')
    .setFontFamily('Arial')
    .setFontSize(10);
}

请注意,我已在

sheet
调用中添加了
getHeaderNumber()
参数。您需要调整该函数以接受并使用
sheet
参数,而不是查找活动工作表。

© www.soinside.com 2019 - 2024. All rights reserved.