2

I have this table and the chart I want as below:

enter image description here

To acheive this I have to switch rows/columns, and use column A as headers.

However I cannot find the corresponding command for the checkbox "Use column A as headers".

This is my script:

function myFunction() {
  const sheetTest = SpreadsheetApp.getActive().getSheetByName("test");

  const numRows = sheetTest.getLastRow();     // e.g. 9
  const numCols = sheetTest.getLastColumn();  // e.g. 3

  const chartRange = sheetTest.getRange(1, 1, numRows, numCols);
  const chart = sheetTest.newChart()
    .asColumnChart()
    .addRange(chartRange)
    .setStacked()
    .setPosition(1, 4, 0, 0)
    .setTransposeRowsAndColumns(true)
    .build();

  sheetTest.insertChart(chart);
}

This is what it produces, just one more step and it'd be perfect as you can see the headers are missing

enter image description here

1 Answer 1

3

In your script, how about using setNumHeaders as follows?

Modified script:

function myFunction() {
  const sheetTest = SpreadsheetApp.getActive().getSheetByName("test");

  const numRows = sheetTest.getLastRow();     // e.g. 9
  const numCols = sheetTest.getLastColumn();  // e.g. 3

  const chartRange = sheetTest.getRange(1, 1, numRows, numCols);
  const chart = sheetTest.newChart()
    .asColumnChart()
    .addRange(chartRange)
    .setStacked()
    .setPosition(1, 4, 0, 0)
    .setTransposeRowsAndColumns(true)
    .setNumHeaders(1) // Added
    .build();

  sheetTest.insertChart(chart);
}

Testing:

When this modified script is used, the following result is obtained.

enter image description here

Reference

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.