我可以使用 python 在 Google 表格中创建图表,但我找不到如何将柱形图的垂直轴刻度设置为对数。可以使用图形编辑器手动执行,但我不知道如何使用 python 程序执行相同的操作。 提前感谢任何帮助。
在现阶段,不幸的是,柱形图纵轴的比例似乎无法通过Sheets API修改。那么,作为未来的请求将此问题报告给 Google 问题跟踪器怎么样? https://issuetracker.google.com/
但是,幸运的是,当使用Google Apps Script时,就可以实现。在这个答案中,作为当前的解决方法,我想建议使用 Google Apps 脚本和 Python 来实现您的目标。
在此解决方法中,使用部署为 Web 应用程序的 Google Apps 脚本修改垂直轴的比例。该脚本可以通过Python脚本运行。
Web Apps 的示例脚本是 Google Apps 脚本。请创建一个 Google Apps 脚本项目。
如果您想直接创建,请访问https://script.new/。在这种情况下,如果您尚未登录 Google,则会打开登录屏幕。请登录谷歌。这样,Google Apps 脚本的脚本编辑器就打开了。
请将以下脚本复制并粘贴到创建的 Google Apps 脚本项目中并保存脚本。
function doGet(e) {
const { spreadsheetId, chartId } = e.parameter;
const sheets = SpreadsheetApp.openById(spreadsheetId).getSheets();
const res = sheets.some(sheet => {
const chart = sheet.getCharts().find(c => c.getChartId() == chartId && c.modify().getChartType() == Charts.ChartType.COLUMN);
if (chart) {
sheet.updateChart(chart.modify().asColumnChart().useLogScale().build());
return true;
}
return false;
});
return ContentService.createTextOutput(res ? "Done." : `Chart with chart ID ${chartId} and COLUMN chart was not found.`);
}
详细信息可以看官方文档。
https://script.google.com/macros/s/###/exec
。
为了测试此 Web 应用程序,请测试以下示例 Python 脚本。请设置您的 Web 应用程序 URL、您的电子表格 ID 和您的图表 ID。图表ID可以通过“方法:spreadsheets.get”获取。
import requests
url = "https://script.google.com/macros/s/###/exec"
url += "?spreadsheetId=###&chartId=###"
res = requests.get(url)
print(res.text)