简体   繁体   中英

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

As described in google app script documentation, to improve performance, it is recommended to perform batch operating by using arrays so that the time for data reading and writing can be reduced significantly with the help of setValues(Array), setFontWeights(Array),setHorizontalAlignments(Array) functions.

I wonder how to set the borders using this technique, as there is no setBorders(Array) function.

If you want to quickly set borders, enable sheet api service and adapt the following script (this is an example)

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);
}

You can simply apply setBorder(and other functions) to a range:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM