使用AppScript的setOption设置图表纵轴格式无效的问题
解决AppScript修改图表纵轴格式不生效的问题
你遇到的问题是用setOption("vAxis", {format: "currency"})无法修改纵轴格式,但修改标题能生效,核心原因是**"currency"不是Google Charts接受的格式参数值**,需要用具体的格式化字符串替代。
修复步骤:
- 把
format: "currency"替换为对应地区的货币格式代码,比如:- 美元格式:
format: "$#,##0.00" - 欧元格式:
format: "€#,##0.00" - 人民币格式:
format: "¥#,##0.00"
- 美元格式:
修复后的完整代码:
function doGet(e) { const { spreadsheetId, chartId } = e.parameter; const sheets = SpreadsheetApp.openById(spreadsheetId).getSheets(); const res = sheets.some(sheet => { // 优化:直接调用getChartType(),无需先执行modify() const chart = sheet.getCharts().find(c => c.getChartId() == chartId && c.getChartType() == Charts.ChartType.COLUMN); if (chart) { // 替换为具体的货币格式字符串 sheet.updateChart(chart.modify().setOption("vAxis", {format: "$#,##0.00"}).build()); return true; } return false; }); return ContentService.createTextOutput(res ? "Done." : `Chart with chart ID ${chartId} and COLUMN chart was not found.`); }
额外说明:
如果需要其他格式,也可以用对应的格式字符串:
- 百分比格式:
format: "#%" - 保留两位小数的数字:
format: "#,##0.00"
内容的提问来源于stack exchange,提问作者Jean Marc Canon
相关产品推荐
相关产品推荐

