问题和解决方法:
看到CellFormat of the official document的时候,我以为textRotation的值可以通过“spreadsheets.get”的方法通过sheets(data(rowData(values(userEnteredFormat(textRotation)))))的字段来获取。但是当我对此进行测试时,没有返回任何值。所以我认为这可能是一个错误。而且,当我在 Google 问题跟踪器中搜索此内容时,我发现了 https://issuetracker.google.com/issues/146274218。从这种情况来看,目前阶段,textRotation的值似乎无法通过Sheets API的“spreadsheets.get”方法获取。
当您想使用脚本检索 textRotation 的值时,作为当前的解决方法,您可以使用由 Google Apps 脚本创建的 Web 应用程序作为包装器来实现它。在这个答案中,我想提出解决方法。
当这种解决方法反映到您的情况的流程中时,它变成如下。
用法:
1。创建 Google Apps 脚本的新项目。
Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。
如果要直接创建,请访问https://script.new/。在这种情况下,如果您没有登录 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 应用程序。
- 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
- 为“执行应用程序为:”选择“我”。
- 为“谁有权访问应用程序:”选择“任何人,甚至是匿名的”。。
- 当然,您可以在这种情况下使用访问令牌。但是在这种情况下,作为一个简单的设置,我使用访问密钥而不是访问令牌。
- 单击“部署”按钮作为新的“项目版本”。
- 自动打开“需要授权”对话框。
- 点击“查看权限”。
- 选择自己的帐户。
- 点击“此应用未验证”中的“高级”。
- 点击“转到###项目名称###(不安全)”
- 点击“允许”按钮。
- 点击“确定”。
- 复制 Web 应用程序的 URL。就像
https://script.google.com/macros/s/###/exec。
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
3。测试。
作为一个简单的测试,当它使用 curl 命令向 Web Apps 请求时,它变为如下。请设置电子表格 ID 和工作表名称。正确部署 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 应用程序中。请注意这一点。
参考资料: