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

如何通过Google Apps Script导出Google Sheets计分卡时保留自定义基线颜色

Google Apps Script导出计分卡图表时自定义基线颜色失效

问题描述

我在Google Sheets中创建了一个计分卡图表,为正负基线值设置了自定义颜色(正基线设为红色),但通过Google Apps Script编程导出该图表时,导出的PNG仅显示默认颜色(正基线为绿色)。

Google Sheets中红色正基线的计分卡图表

预期结果

导出的PNG文件中,正基线值应显示为红色。

实际结果

导出的PNG文件中,正基线值显示为默认的绿色。

导出的PNG中正基线为绿色的计分卡

复现步骤

  1. 创建新的Google Sheets电子表格,填入以下数据:
    ValueBaseline
    10050
  2. 选中这四个数据单元格,点击顶部菜单的Insert > Chart。
  3. 在Chart editor面板的Setup标签页中,从Chart type下拉列表选择Scorecard chart。
  4. 在Chart editor面板的Customize标签页的Baseline Value部分,将Positive color改为红色,Negative color改为绿色。
  5. 在Chart editor面板的Customize标签页的Chart & axis titles部分,将图表标题设置为“Scorecard with red baseline color”。
  6. 点击顶部菜单的Extensions > Apps Script。
  7. 将Code.gs文件内容替换为以下代码:
const sheetName = 'Sheet1'

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Charts')
    .addItem('Export to Drive', 'exportCharts')
    .addToUi();
}

function exportCharts() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = spreadsheet.getSheetByName(sheetName);
  const charts = sheet.getCharts();

  charts.forEach((chart) => {
    const image = chart.getAs('image/png');
    const title = chart.getOptions().get('title');
    DriveApp.createFile(image.setName(`${title}.png`));
  });
}
  1. 在左侧菜单的Services处点击**+符号,添加Drive**服务。
  2. 点击磁盘图标保存脚本,返回Google Sheet并刷新页面。
  3. 页面加载几秒后,顶部菜单会出现新选项;点击Charts > Export to Drive(会提示授予脚本访问Google Drive的权限)。
  4. 在Google Drive中搜索名为“Scorecard with red baseline color.png”的文件,打开后确认基线颜色为绿色而非红色。

内容的提问来源于stack exchange,提问作者Shaun Scovil

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 09:52:55