简体   繁体   English

如何使用带有数组的批处理技术将数据分组到 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?

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.如google app脚本文档中所述,为了提高性能,建议使用数组进行批量操作,这样可以在setValues(Array)、setFontWeights(Array)、setHorizo​​ntalAlignments的帮助下显着减少数据读写的时间(数组)函数。

I wonder how to set the borders using this technique, as there is no setBorders(Array) function.我想知道如何使用这种技术设置边框,因为没有 setBorders(Array) 函数。

If you want to quickly set borders, enable sheet api service and adapt the following script (this is an example)如果你想快速设置边框,启用 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);
}

You can simply apply setBorder(and other functions) to a range:您可以简单地将 setBorder(和其他函数)应用于一个范围:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Google Sheets App Script 中的“setFontWeights(Array)”函数等批量操作不起作用? - Why is batch operating in Google Sheets App Script such as "setFontWeights(Array)" function not working? 批量操作Google App脚本 - Batch operating Google App Script 如何使用Google表格中的应用脚本自动复制/粘贴数据 - How to automatically copy/paste data using App Script in Google Sheets Google Sheets Script - 使用数组进行数据验证 - Google Sheets Script - using an array for data validation 谷歌表格脚本超过最大执行时间 - Google sheets script Exceeded maximum execution time 如何在从 API 端点获取数据到 Google 表格时处理 Google Apps 脚本 6 分钟执行时间限制? - How to handle Google Apps script 6 minute execution time limit while fetching data from an API endpoint to google sheets? 通过数组/缓存减少执行时间的代码??? 谷歌应用脚本 - Code to reduce execution time by array / caching??? Google App Script 如何减少此脚本的执行时间(Google应用脚本超时) - How to reduce execution time of this script (Google app script timeout) 提高 Google Sheets 中 App Script 代码的加载效率 - Improve loading efficiency of App Script code in Google Sheets 使用数组减少Google Apps脚本的执行时间? - Reducing Google Apps Script execution time with using an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM