简体   繁体   English

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

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

I'm using the Google Sheets API (v4) to create/update spreadsheets programmatically and have run into the following issue:我正在使用 Google Sheets API (v4) 以编程方式创建/更新电子表格并遇到以下问题:

As per the documentation ( https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells#CellFormat ), I'm setting the number format to CURRENCY ;根据文档( https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells#CellFormat ),我将数字格式设置为CURRENCY this will correctly display the number as a numeric value with a ¥ sign at the front (Japanese locale).这将正确地将数字显示为前面带有 ¥ 符号的数值(日语区域设置)。 However, it does not seem to actually select the "Currency" option on the formats dropdown, and more importantly, does NOT reflect the specified format when downloading the spreadsheet (eg as an.xlsx file).但是,它似乎实际上并没有 select 格式下拉菜单上的“货币”选项,更重要的是,在下载电子表格时不反映指定的格式(例如,作为.xlsx 文件)。

在此处输入图像描述

This is different from selecting the 'Currency' option manually (via the UI), in which case values are correctly displayed once downloaded.这与手动(通过 UI)选择“货币”选项不同,在这种情况下,一旦下载,值就会正确显示。

在此处输入图像描述

Here's the relevant section of code:这是代码的相关部分:


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

I've also tried settings the pattern (tried few different ones), but haven't been able to actually set the cell format, despite the value being displayed as such.我也尝试过设置模式(尝试了几个不同的模式),但无法实际设置单元格格式,尽管值是这样显示的。

Probably a simple mistake, but I've been stuck on this for a while.. any help would be greatly appreciated!可能是一个简单的错误,但我已经坚持了一段时间......任何帮助将不胜感激!

In that case, I thought that the property of pattern might be required to be set.在那种情况下,我认为可能需要设置pattern的属性。 So in this case, how about modifying your request of repeatCell as follows?那么在这种情况下,如何修改您的repeatCell请求如下?

Modified request:修改请求:

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

Note:笔记:

  • In my environment, when above modified request is used for the batchUpdate method, I could confirm that "Currency" was checked.在我的环境中,当上述修改请求用于 batchUpdate 方法时,我可以确认“货币”已被选中。

References:参考:

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

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