Reading title of Graph

2019-07-27 22:07发布

问题:

Using Google Script, is it possible to read the title of a graph in google spreadsheets?

What I like to do is to place a specific graph from Google Spreadsheet into a Google Document in a specific location.

So far I can place a graph from Google Spreadsheet into a Google Document in a prespecified location. However, I do not yet control which graph comes where.

function test() {
  SpreadsheetApp.flush();
  var Grafieken = SpreadsheetApp.getActiveSheet().getCharts();
  var targetDoc = DocumentApp.openById(DOC_id);
  var DBody = targetDoc.getBody();
  var Grafiek;
  for (var i in Grafieken)
    Grafiek = Grafieken[i];
    var locatie =  DBody.findText('%Grafiek'+i+'%').getElement().getParent().asParagraph().appendInlineImage(Grafiek);

  }
}

For the script to work for me, I need to know which graph is which. I thought, that would be the easiest way to maintain to use the Graph Title for this. Any suggestions?

回答1:

To get the chart title use the call getOptions().get('title').

Check the code bellow

function test() {
  SpreadsheetApp.flush();
  var Grafieken = SpreadsheetApp.getActiveSheet().getCharts();
  var targetDoc = DocumentApp.openById(DOC_id);
  var DBody = targetDoc.getBody();
  var Grafiek;
  for (var i in Grafieken)
    Grafiek = Grafieken[i];

   // Get chart title 
   var charTitle = Grafiek.getOptions().get("title");

   var locatie =  DBody.findText('%'+charTitle+'%').getElement().getParent().asParagraph().appendInlineImage(Grafiek);

}