apps script

Create Table in Google Doc Using Google Apps Script

Here we will learn how to create a table inside a Google document using Google apps script, This is a straightforward code which creates a table in Google Document.

We need to write a function. Likewise, I have created a function and called this createTable().

function createTable(){
 //Get Active Document
 var doc=DocumentApp.getActiveDocument();

 //Get Document Body
 var body=doc.getBody();

 //Add Table
 var table=body.appendTable();

 //Create 10 rows and 5 columns
 var numRows=10;
 var numColumns=5;

 for(var i=0;i < numRows;i++){
  var tr=table.appendTableRow();
  
  //Add cells in each row with blank values
  for(var j=0;j < numColumns;j++){
   var tb=tr.appendTableCell('');
  }
 }

}

You can run this script by selecting function name createTable from the drop down and then click on play button as highlighted in Script in below image.

Moreover, it will create a table with blank values in each cell.

You can also apply styles to table cells. Rather, below code is applying light grey color to the cell, font bold, foreground color black and horizontal alignment left.

  var cellFormatting = {}; 
  cellFormatting[DocumentApp.Attribute.BACKGROUND_COLOR] = '#fce5cd'; 
  cellFormatting[DocumentApp.Attribute.BOLD] = true; 
  cellFormatting[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000'; 
  cellFormatting[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT 

After adding this cell formatting code to create table function the entire code will look as below

function createTable(){
  var cellFormatting = {}; 
  cellFormatting[DocumentApp.Attribute.BACKGROUND_COLOR] = '#fce5cd'; 
  cellFormatting[DocumentApp.Attribute.BOLD] = true; 
  cellFormatting[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000'; 
  cellFormatting[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT 
  
  //Get Active Document
  var doc=DocumentApp.getActiveDocument();

  //Get Document Body
  var body=doc.getBody();

  //Add Table
  var table=body.appendTable();

  //Create 10 rows and 5 columns
  var numRows=10;
  var numColumns=5;

  for(var i=0;i < numRows;i++){
    var tr=table.appendTableRow();
  
    //Add cells in each row with blank values
    for(var j=0;j < numColumns;j++){
      var td=tr.appendTableCell('Enter Value Here'); 
      td.setAttributes(cellFormatting);
    }
  }

}

Create Table On Google Doc from data on Google Spreadsheet

Google docs spreadsheet

Firstly, Open source spreadsheet, you can use  SpreadsheetApp.openById or SpreadsheetApp.openByUrl in script to open the sheet.

Secondly,  you need to get the sheet which is having the source data, Suppose, source data is on the tab ‘TableData’ and range address A1: D10.

function createTableFromSheet(){
//Open source spreadsheet having sample table to be imported on doc.
var ss=SpreadsheetApp.openById('1XfrbhxTmwiKc6il7Kl7uT4qgSK5J090sCwVbjQBAYzo'); //Replace this Id with your sheet Id
var sh=ss.getSheetByName('TableData');
var values=sh.getRange('A1:D10').getValues(); //Set your Data range here

//Get Active Document
var doc = DocumentApp.getActiveDocument();

//Get Body
var body = doc.getBody();

//Add table
var table = body.appendTable(values);
}

Add a Comment

Your email address will not be published. Required fields are marked *