Google Sheet API 中从未设置 TextRotation 角度

TextRotation angle is never set in Google Sheet API

我正在使用来自 Java 客户端的 Google Sheets API v4。我正在尝试从 Cell class 获取格式。一切顺利。

文本旋转

我成功地获得了设置了垂直 属性 的 TextRotation element,但是 angle 属性 从未设置过,无论我输入什么在来源 Google Sheet 的文档中。

无论在 Google Sheet 应用程序中选择什么角度值,都不会设置角度 属性。

我已经尝试检查返回的 JSON,以了解问题是否出在 Java 客户端。
但是 JSON REST API 也从来没有 returns 这个 属性.

当前结果:
.
预期结果:

问题和解决方法:

当我看到CellFormat of the official document, I thought that the value of textRotation can be retrieved by the fields of sheets(data(rowData(values(userEnteredFormat(textRotation))))) when the method of "spreadsheets.get" is used. But when I tested this, no values are returned. So I thought that this might be a bug. And, when I searched this at the Google issue tracker, I found https://issuetracker.google.com/issues/146274218的时候。从这种情况来看,在现阶段,似乎无法通过Sheets API.

的“spreadsheets.get”方法获取textRotation的值

当您想使用脚本检索 textRotation 的值时,作为当前的解决方法,您可以使用由 Google Apps 脚本创建的 Web 应用程序作为包装器来实现它。在这个答案中,我想提出解决方法。

当此解决方法反映到您的情况的流程中时,它会变成如下。

用法:

1。创建 Google Apps 脚本的新项目。

Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。

如果要直接创建,请访问https://script.new/。在这种情况下,如果您未登录 Google,则会打开登录屏幕。所以请登录Google。这样,Google Apps Script 的脚本编辑器就打开了。

2。准备 Web 应用程序端。 (服务器端)

请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。此脚本用于 Web 应用程序。此 Web 应用程序用作 API.

服务器端:Google Apps 脚本

function doGet(e) {
  const key = "sampleKey";
  const id = e.parameter.spreadsheetId;
  const sheetName = e.parameter.sheetName;
  if (e.parameter.key != key || !id || !sheetName) {
    return ContentService.createTextOutput(JSON.stringify({message: "Error."})).setMimeType(ContentService.MimeType.JSON);
    }

  const sheet = SpreadsheetApp.openById(id).getSheetByName(sheetName);
  const textRotations = sheet.getDataRange().getTextRotations().map(r => r.map(c => ({angle: c.getDegrees(), vertical: c.isVertical()})));
  return ContentService.createTextOutput(JSON.stringify(textRotations)).setMimeType(ContentService.MimeType.JSON);
}

3。部署 Web 应用程序。

  1. 在脚本编辑器上,通过“发布”->“部署为网络应用程序”打开一个对话框。
  2. Select “我” for “执行应用程序:”
    • 至此,脚本运行作为所有者。
  3. Select “任何人,甚至是匿名的” for “有权访问该应用程序的人:”.
  • 当然,对于这种情况,你可以使用access token。但是在这种情况下,作为一个简单的设置,我使用访问密钥而不是访问令牌。
  1. 单击“部署”按钮作为新的“项目版本”。
  2. 自动打开“需要授权”的对话框。
    1. 单击“查看权限”。
    2. Select自己的账号。
    3. 点击“此应用未验证”处的“高级”。
    4. 点击“转到###项目名称###(不安全)”
    5. 单击“允许”按钮。
  3. 点击“确定”。
  4. 复制 Web 应用程序的 URL。就像 https://script.google.com/macros/s/###/exec
    • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

3。测试中。

作为一个简单的测试,当它使用curl命令向Web Apps请求时,变成如下。请设置传播sheet ID和sheet名称。正确部署 Web 应用程序后,将返回值。

$ curl -L "https://script.google.com/macros/s/###/exec?spreadsheetId=###&sheetName=Sheet1&key=sampleKey"
结果:

数据范围用于检索值。因此,例如,当将值设置为“Sheet1”中的单元格“A1:C3”时,将返回以下结果。

[
    [{"angle":30,"vertical":false},{"angle":0,"vertical":true},{"angle":0,"vertical":false}],
    [{"angle":30,"vertical":false},{"angle":30,"vertical":false},{"angle":0,"vertical":false}]
]
  • 在这种情况下,单元格“A1、A2、B2”的文本旋转了 30 度。并且,单元格“B1”具有垂直方向。

注:

  • 当您修改Web Apps的脚本时,请重新部署Web Apps为新版本。由此,最新的脚本被反​​映到Web Apps。请注意这一点。

参考文献:

最后,我写了一个 JS 库:screen-rotation.js 来管理所有这些东西。

screenorientation().change(function(){
 // My event management
});

它支持 iOS、Android 和桌面浏览器