Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created August 19, 2020 05:35
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/6ec4f2278510311ea06b838c69828692 to your computer and use it in GitHub Desktop.
Save tanaikech/6ec4f2278510311ea06b838c69828692 to your computer and use it in GitHub Desktop.
Converting Range in Google Spreadsheet as Image using Google Apps Script

Converting Range in Google Spreadsheet as Image using Google Apps Script

This is a sample script for converting a range in Google Spreadsheet as an image data using Google Apps Script. Unfortunately, there are no methods for directly converting the range in Google Spreadsheet as an image data in the built-in functions. So in this case, as a workaround, Charts Service is used.

Sample script

const range = "B5:D10";
const [header, ...values] = SpreadsheetApp.getActiveSheet()
  .getRange(range)
  .getDisplayValues();
const table = Charts.newDataTable();
header.forEach((e) => table.addColumn(Charts.ColumnType.STRING, e));
values.forEach((e) => table.addRow(e));
const blob = Charts.newTableChart()
  .setDataTable(table.build())
  .setDimensions(500, 500)
  .setOption("alternatingRowStyle", false)
  .build()
  .getBlob();
  • In this sample script, the range of "B5:D10" of the active sheet is converted to an image data which is PNG format.

Result

When above blob is saved as a PNG file, the following result is obtained.

References

@zarmeenlakhany
Copy link

zarmeenlakhany commented Sep 30, 2023

Will this have the data range formatting available?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment