以编程方式检索 Google Sheets 单元格边框样式 [英] Retrieving Google Sheets cell border style programmatically

查看:35
本文介绍了以编程方式检索 Google Sheets 单元格边框样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以设置范围边框样式和颜色.现在的问题是:我们如何以编程方式获取边框样式?

It is possible to set the range border style and color. Now the question is: how can we get the border style programmatically?

我正在寻找类似的东西:

I'm looking for something like:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var b2 = sheet.getRange("B2");

var border = b2.getBorder(); // getBorder does not exist, but b2.setBorder does :-/

推荐答案

我不确定这是否是最好的方法.我总是使用 Sheets API 检索边框样式.示例脚本如下.

I'm not sure whether this is the best way. I always retrieve the border style using Sheets API. The sample script is as follows.

使用此示例脚本时,请启用 Sheets API.

When you use this sample script, please enable Sheets API.

自 2019 年 4 月 8 日起,2019 年 4 月 8 日之后创建的 GAS 项目启用 API 的规范已更改.参考

From April 8, 2019, the specification for enabling API has been changed for the GAS project created after April 8, 2019. Ref

在这种情况下,当在高级 Google 服务中启用 API 时,API 会在 API 控制台中自动启用.因此,您只需在高级 Google 服务中启用该 API,即可使用该 API.

In this case, when the API is enabled at Advanced Google services, the API is automatically enabled at API console. So you can use the API by only enabling it at Advanced Google services.

  • 在脚本编辑器上
    • 资源 -> 高级 Google 服务
    • 启用 Google Sheets API v4

    在这种情况下,需要在高级 Google 服务和 API 控制台中启用 API.

    In this case, the API is required to be enabled at Advanced Google services and API console.

    • 在脚本编辑器上
      • 资源 -> 高级 Google 服务
      • 启用 Google Sheets API v4
      • 在脚本编辑器上
        • 资源 -> 云平台项目
        • 查看 API 控制台
        • 在入门中,点击探索并启用 API".
        • 在左侧,点击图书馆.
        • 在搜索 API 和服务,输入表".然后点击 Google Sheets API.
        • 点击启用按钮.
        • 如果 API 已经启用,请不要关闭.

        如果您现在打开带有使用 Sheets API 的脚本的脚本编辑器,您可以通过访问此 URL https://console.cloud.google.com/apis/library/sheets.googleapis.com/

        If now you are opening the script editor with the script for using Sheets API, you can enable Sheets API for the project by accessing this URL https://console.cloud.google.com/apis/library/sheets.googleapis.com/

        如果您想使用 2019 年 4 月 8 日之后创建的 Advanced Google services at GAS 项目中未包含的 API,则需要将 Cloud Platform 项目链接到 Google Apps 脚本项目.并且需要在 API 控制台启用 API.您可以在此处查看详细流程.

        If you want to use the API which is not included in the Advanced Google services at GAS project created after April 8, 2019, it is required to link Cloud Platform Project to Google Apps Script Project. And the API is required to be enabled at API console. You can see the detail flow of this at here.

        var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
        var res = Sheets.Spreadsheets.get(spreadsheetId, {ranges: "Sheet1!B2", fields: "sheets/data/rowData/values/userEnteredFormat/borders"});
        

        参考:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆