Spencer
Spencer

Reputation: 455

Google Apps Script insert Sheets data to Doc throwing error

Using the code in the Google Developer help center to insert a table into a document works fine.


// Create a two-dimensional array containing the cell contents.
var cells = [
  ['Row 1, Cell 1', 'Row 1, Cell 2'],
  ['Row 2, Cell 1', 'Row 2, Cell 2']
];

// Build a table from the array.
body.appendTable(cells);

However, what I need to do is insert cells from a spreadsheet as a table to my document. I've tried following other posts from here and wrote this:

const body = DocumentApp.getActiveDocument().getBody();
const sheet = SpreadsheetApp.openById("spreadsheetID").getSheetByName("Reports");
var ssCells = sheet.getRange("A2:C5").getValues();
body.appendTable(ssCells);

but it throws the error "Exception: The parameters (number[]) don't match the method signature for DocumentApp.Body.appendTable." I've logged the ssCells variable and it returns a normal 2d array of values, which looks in format identical to the cells variable from the Google example.

How do I get past this error and insert Sheets data as a table to Docs?

Upvotes: 0

Views: 259

Answers (1)

Spencer
Spencer

Reputation: 455

One of my Sheets columns has numerical values which killed the whole thing. Using .getDisplayValues() to return the String versions and it works.

const body = DocumentApp.getActiveDocument().getBody();
const sheet = SpreadsheetApp.openById("spreadsheetID").getSheetByName("Reports");
var ssCells = sheet.getRange("A2:C5").getDisplayValues();
body.appendTable(ssCells);

Upvotes: 1

Related Questions