如何使用带有数组的批处理操作技术将数据分组到 Google Sheets App Script 中的表中以缩短执行时间?

How to group data into tables in Google Sheets App Script using Batch Operating technique with Array to improve execution time?

如googleapp脚本文档所述,为提高性能,建议使用数组进行批量操作,借助setValues(数组)、setFontWeights(数组)、setHorizo​​ntalAlignments(数组)函数。

我想知道如何使用这种技术设置边框,因为没有 setBorders(Array) 函数。

如果要快速设置边框,启用sheet api服务并适配以下脚本(此为示例)

function setBorderCells() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const sh = ss.getActiveSheet()
  bordersUpdating(ss.getId(), sh.getSheetId(), 2, 6, 7, 8)
}
function bordersUpdating(id, gid, startRow, endRow, startColumn, endColumn) {
  const resource = {
    "requests": [
      {
        "updateBorders": {
          "range": {
            "sheetId": gid,
            "startRowIndex": +startRow - 1,
            "endRowIndex": +endRow,
            "startColumnIndex": +startColumn - 1,
            "endColumnIndex": +endColumn
          },
          "top": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "bottom": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "left": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "right": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "innerHorizontal": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
          "innerVertical": {
            "style": "SOLID",
            "width": 1,
            "color": {
              "blue": 1.0
            },
          },
        }
      }
    ]
  }
  Sheets.Spreadsheets.batchUpdate(resource, id);
}

您可以简单地将 setBorder(和其他函数)应用于一个范围:

SpreadsheetApp.getActive().getSheetByName("Log")
    .getRange(1,1,5,5)
    .setBorder(true,true,true,true,true,true,
        "green",SpreadsheetApp.BorderStyle.DASHED)