0

我每天晚上都安排了一个脚本来查看许多不同的工作表并在任何新数据条目周围绘制边框。

该脚本有效但不一致。当我检查它完全适用于某些 google 表格和部分适用于其他表格时。每个 google 电子表格上大约有 10 个内部工作表,并且为某些人绘制了边框,而不为其他人绘制了边框。

我不明白为什么它运行不干净。

function addBorders() {
  //Access the sheet which contains all the IDs
  var idSheet = SpreadsheetApp.openById('example');

  //Check for the last row
  var lastRow = 0;
  var currRow = 2;
  //Loop through the sheet and find the last ID available
  while (idSheet.getRange("A" + currRow).getValue() != "")
  {
    currRow = currRow + 1;
  }
  //Set the last row with an ID found to be the lastRow
  lastRow = currRow;
  idSheet.getRange("B1").setValue("Total Rows");
  idSheet.getRange("B2").setValue(lastRow);

  //Loop through the ID sheet to find the ID to make changes to
  for( var y = 2; y < lastRow; y++) 
  { 
    var studentID = idSheet.getSheetByName("Sheet1").getRange(y, 1).getValue();

    //As IDs are take from the ID sheet, open the relevant sheet and run the code below
    var ss = SpreadsheetApp.openById(studentID);
    var sheetsCount = ss.getNumSheets();
    var sheets = ss.getSheets();

    //For each sheet in the individal student spreadsheets, set the borders correctly
    for (var i = 0; i < sheetsCount; i++)
    {
      var sheet = sheets[i]; 
      var range = sheet.getRange(6, 3, 35);
      var values = range.getValues().map(function(d){ return d[0] });

      //clear previous border
      var selection = sheet.getRange(6,2,35,5)
      selection.setBorder(false,false,false,false,false,false);  

      //set border
      var index = values.indexOf("");

      var border = sheet.getRange(5, 2, index+1, 5);
      border.setBorder(true, true, true, true, true, true);
    }
  }
}
4

1 回答 1

0

这是不正确while (idSheet.getRange("A" + currRow).getValue() != "")的,您必须指定一个工作表(即选项卡)示例:idSheet.getRange( "Sheet1!A" + currRow).getValue()!="")如果这有效,那么它可能总是使用 ss.getSheets()[0] 最左边的工作表。

这将导致无法访问最后一行的数据

for( var y = 2; y < lastRow; y++) 

你怎么知道下面的索引永远不会是-1。如果是则 index + 1 = 0 这不是合法的行值。对我来说似乎有点片状。

var index = values.indexOf("");
var border = sheet.getRange(5, 2, index+1, 5); 
于 2019-02-20T06:41:53.457 回答