You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 21:36:08