我正在尝试循环遍历 Sheets 和 getLastColumn()。我激活第一个 Sheet 并获取最后一列,然后激活下一个 Sheet,但这次我在 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
代码无法正常工作,因为
for (a
循环迭代三次,但代码仅尝试处理两张表。使用更结构化的方法,可能像这样:请注意,我已向调用添加了一个
sheet
参数getHeaderNumber()
。您需要调整该函数以接受和使用该sheet
参数,而不是使用或类似方法定位工作表Spreadsheet.getActiveSheet()
。参见Array.forEach()。