繁体   English   中英

使用 Google 表格 API (v4) 格式化单元格

[英]Formatting cells with the Google Sheets API (v4)

我正在使用 Google Sheets API (v4) 以编程方式创建/更新电子表格并遇到以下问题:

根据文档( https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells#CellFormat ),我将数字格式设置为CURRENCY 这将正确地将数字显示为前面带有 ¥ 符号的数值(日语区域设置)。 但是,它似乎实际上并没有 select 格式下拉菜单上的“货币”选项,更重要的是,在下载电子表格时不反映指定的格式(例如,作为.xlsx 文件)。

在此处输入图像描述

这与手动(通过 UI)选择“货币”选项不同,在这种情况下,一旦下载,值就会正确显示。

在此处输入图像描述

这是代码的相关部分:


import { google, sheets_v4 } from 'googleapis';

const sheetsApi = google.sheets({
  version: 'v4',
  auth: await this.getAuthClient(),
});

await sheetsApi.spreadsheets
  .batchUpdate({
    spreadsheetId: resource,
    requestBody: {
      requests: [
        {
          updateSpreadsheetProperties: {
            fields: 'locale',
            properties: {
              locale: 'ja',
            },
          },
        },

        ...,
        
        {
          repeatCell: {
            fields: 'userEnteredFormat.numberFormat',
            cell: {
              userEnteredFormat: {
                numberFormat: { type: 'CURRENCY' },
              },
            },
          },
        },
      ],
    },
  })
  .catch((error) => {
    console.log(error);
  });

我也尝试过设置模式(尝试了几个不同的模式),但无法实际设置单元格格式,尽管值是这样显示的。

可能是一个简单的错误,但我已经坚持了一段时间......任何帮助将不胜感激!

在那种情况下,我认为可能需要设置pattern的属性。 那么在这种情况下,如何修改您的repeatCell请求如下?

修改请求:

{
  "repeatCell": {
    "range": {,,,}, // Please set range.
    "cell": {
      "userEnteredFormat": {
        "numberFormat": {
          "type": "CURRENCY",
          "pattern": "[$¥-411]#,##0.00"  // <--- Added
        }
      }
    },
    "fields": "userEnteredFormat.numberFormat"  // or "fields": "userEnteredFormat"
  }
}

笔记:

  • 在我的环境中,当上述修改请求用于 batchUpdate 方法时,我可以确认“货币”已被选中。

参考:

暂无
暂无

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

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