【问题标题】:Column style ignored using spreadsheets.batchUpdate with AppendCellsRequest使用带有 AppendCellsRequest 的电子表格.batchUpdate 忽略列样式
【发布时间】:2020-04-09 15:02:04
【问题描述】:

作为开发人员,我使用此端点:https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate

并要求: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#AppendCellsRequest 插入具有值的新行。

我有一些列格式样式的电子表格:背景颜色/字体重量/文本换行/数据验证。

问题,当我使用这样的请求时:

curl --request POST \
 'https://sheets.googleapis.com/v4/spreadsheets/1SoDx8YRyiKF9vKfa_2w0xc7DTNIlQoLC6hBq1SCJEJY:batchUpdate?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  --data '{"requests":[{"appendCells":{"sheetId":0,"fields":"*","rows":[{"values":[{"userEnteredValue":{"stringValue":"TEST STRING VALUE long"}},{"userEnteredValue":{"stringValue":"TEST TEXT LONG"}}]}]}}]}' \
  --compressed

我看到文本换行和数据验证样式被忽略了:

这里我只使用的关键点:'userEnteredValue' 字段来提供值。

根据文档: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells#CellData 我可以使用'userEnteredFormat'/'dataValidation',但是“写入时,新格式将与现有格式合并。”我发现再次获取和发布这些样式格式非常复杂。

我的问题 - 有没有办法尽可能简单地使用 AppendCellsRequest 发布 CellData 并保留现有的列样式?

【问题讨论】:

  • 我为我糟糕的英语水平道歉。不幸的是,我无法从您的图片中了解I see that text-wrap and data-validation styles are ignored:。可以问一下具体情况吗?
  • 嗨!更新了我的描述。如您所见,第一列缺少验证“警告”,并且两列都忽略了文本换行
  • 感谢您的回复和补充信息。根据您的附加信息,我提出了一个修改点作为答案。你能确认一下吗?如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

标签: google-sheets-api


【解决方案1】:
  • 您希望使用 Sheets API 中的 batchUpdate 方法的 AppendCellsRequest 来放置值。
  • 您希望在 AppendCellsRequest 运行时保留单元格的文本样式和数据验证规则。
  • 您只想放置文本值。
  • 您已经能够使用 Sheets API 为 Google 电子表格获取和输入值。

修改点:

在您的情况下,您使用"fields":"*",而您只设置userEnteredValue 的属性。在这种情况下,还设置了除userEnteredValue 之外的属性。我认为这是您的问题的原因。

为避免此问题,请使用userEnteredValuefields

修改后的请求正文:

当你的请求正文被修改后,变成如下。

从:
--data '{"requests":[{"appendCells":{"sheetId":0,"fields":"*","rows":[{"values":[{"userEnteredValue":{"stringValue":"TEST STRING VALUE long"}},{"userEnteredValue":{"stringValue":"TEST TEXT LONG"}}]}]}}]}'
到:
--data '{"requests":[{"appendCells":{"sheetId":0,"fields":"userEnteredValue","rows":[{"values":[{"userEnteredValue":{"stringValue":"TEST STRING VALUE long"}},{"userEnteredValue":{"stringValue":"TEST TEXT LONG"}}]}]}}]}'
  • "fields":"*" 修改为 "fields":"userEnteredValue"

参考资料:

【讨论】:

    【解决方案2】:

    田池的回答是正确的,但我发现以下奇怪之处。

    我的CellData 看起来像(Kotlin):

            fun cellDataDouble(value: Double, doublePattern: String = "#.#"): CellData {
                return CellData().also { cd ->
                    cd.userEnteredFormat = CellFormat().setNumberFormat(NumberFormat().also { nf ->
                        nf.type = "Number"
                        nf.pattern = doublePattern
                    })
                    cd.userEnteredValue = ExtendedValue().setNumberValue(value)
                }
            }
    

    所以对我来说将fields 设置为"userEnteredValue,userEnteredFormat" 似乎是合乎逻辑的,但如果你这样做,列中的其他格式将被删除。行为与fields="*" 相同。这显示在下面的测试用例屏幕截图中:

    第 20 行附加了指定的两个字段,第 21 行仅附加了 userEnteredValue,这表明未应用 userEnteredFormat。文档建议应该合并格式,但很明显,只需指定数字格式即可删除字体和粗体格式。

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多