how to run a menu item script across selected shee

2019-07-09 23:49发布

I have been trying to accomplish running this border script across only certain sheet tabs, and I have been unsuccessful at calling this across them. the sheet tabs are as follows. Current Sales, Pending Orders, Subcontract Orders, Partial Shipped, Quotes, Archived Sales.

function setborders() {
 var menu = [{name: "Borders for nonempty cells", functionName: "borders"}];
 SpreadsheetApp.getActiveSpreadsheet().addMenu("Set Borders", menu);
}

function borders() {
  var ss = SpreadsheetApp.getActive();
  var numsheets = ss.getNumSheets();
  var allsheets = ss.getSheets();
  for (var s in allsheets){
    var sheet=allsheets[s]
    var range = sheet.getRange("A2:V1000");
    var values = range.getValues();
    var offsetRow = range.getRowIndex();
    
    for(var i = 0; i < allsheets.length; i++) 
   if(['Current Sales','Pending Orders','Subcontract Orders','Partial Shipped','Quotes','Archived Sales'].indexOf(allsheets[i].getName())){
  
  if(sheet.getRange(range.getRow(), 1).getValue()) {
    range.setBorder(false, false, false, false, false, false);
    SpreadsheetApp.flush();
    for (var i = 0; i < values.length; i++) {
      if (values[i][0]) { // Only for non-empty A column cells
        sheet.getRange("A" + (i + offsetRow) + ":V" + (i + offsetRow)).setBorder(true, true, true, true, false, false, "black",SpreadsheetApp.BorderStyle.SOLID);
      }
    }
       
    for (i = 0; i < values.length; i++) {
      if (values[i][0] && values[i][14]) { 
        sheet.getRange("O" + (i + offsetRow) + ":O" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
      }
    }
    
    for (i = 0; i < values.length; i++) {
      if (values[i][0] && values[i][18]) { 
        sheet.getRange("S" + (i + offsetRow) + ":S" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
      }
    }
    for (i = 0; i < values.length; i++) {
      if (values[i][0] && values[i][19]) { 
        sheet.getRange("T" + (i + offsetRow) + ":T" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
      }
    }
    for (i = 0; i < values.length; i++) {
      if (values[i][0] && values[i][20]) { 
        sheet.getRange("U" + (i + offsetRow) + ":U" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
      }
    }
    for (i = 0; i < values.length; i++) {
      if (values[i][0] && values[i][21]) { 
        sheet.getRange("V" + (i + offsetRow) + ":V" + (i + offsetRow)).setBorder(true, true, true, true, true, true, "black",SpreadsheetApp.BorderStyle.SOLID_THICK);
      }
    }
  }
}
}
}

1条回答
Summer. ? 凉城
2楼-- · 2019-07-10 00:17

Revision 5

The "fix" didn't work because the code has nested fors, both iterating through all sheets. The inner for has the suggested condition but the outer for not.

Revision 1

To limit the execution certain parts of the code to specific spreadsheet you could use the getName() method of Class Sheet and compare it with the list of the valid sheets. One way of doing this is creating an array of the valid sheet names an use indexOf.

Let say that the valid sheet names are Sheet1 and Sheet3, and that sheets is a collection of all the sheets resulting from something like var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();), then the following code will iterate over all the sheets, but only execute one part of the code on the valid sheets:

for(var i = 0; i < sheets.length; i++) 
   if(['Sheet1','Sheet3'].indexOf(sheets[i].getName())){
     //Add here the code to be executed on the valid sheets
   }
}
查看更多
登录 后发表回答