Titles in Google Sheets Charts

谁都会走 提交于 2020-08-08 05:15:23

问题


When I first create a chart in Google Sheets, the Title is in a movable box that I can select and drag around inside the chart box.

If I then change that title programmatically, e.g.

        chart = chart.modify()
        .setOption('title',ChartTitleNameReplacement)
        .build();
        selectedSheet.updateChart(chart); 

the Title jumps up to the top of the chart box and cannot be dragged anywhere.

What option setting do I need in the Google Apps Script code to keep that box movable? Alternatively, is there any way of specifying a position in the chart box for the Title?


回答1:


I have experienced the same issue with your situation. I think that this might be a bug. In this case, I achieved to modify the title without locking the title position using Sheets API. The sample script is as follows.

Sample script:

Before you run the script, please enable Sheets API at Advanced Google services.

const updatedTitle = "### updated title ###";

const ss = SpreadsheetApp.getActiveSpreadsheet()
const ssId = ss.getId();
const chart = Sheets.Spreadsheets.get(ssId).sheets[0].charts[0];
delete chart.position;
chart.spec.title = updatedTitle;
Sheets.Spreadsheets.batchUpdate({requests: [{updateChartSpec: chart}]}, ssId);
  • In this case, the title of the 1st chart in the 1st sheet in the active Google Spreadsheet is modified.

Note:

  • When only the title is modified, an error like the internal error occurs. At UpdateChartSpecRequest, there is no fields. I think that this might be the reason of the issue. So I added the existing properties to the request body by modifying the title. Please be careful this.
  • In the current stage, when the title is updated, it seems the title position is reset.
  • This is a simple sample script for modifying the chart title without locking the title position. So please modify this for your actual situation.
  • I couldn't find this issue at the issue tracker. So how about reporting it?

References:

  • Advanced Google services
  • Method: spreadsheets.get
  • Method: spreadsheets.batchUpdate
  • UpdateChartSpecRequest


来源:https://stackoverflow.com/questions/62059874/titles-in-google-sheets-charts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!