jmt
jmt

Reputation: 751

Google Script different aligment in same table row in Docs table

I'm trying to put three paragraphs (loaded from google spreadsheet) into docs via google app script.

One should be on its own row and the other two should be on same row but with different alignment.

Pic. 1 - What I want

enter image description here

The problem is, google allow only appending paragraphs into table cells. And paragraph contains new line so the second text is on the new line.

Pic. 2 - What I get

enter image description here

I've tried appending paragraph and then appending text. But I don't know how to set up right-aligned tab and insert tab after first text.

Is it even possible to set up tabs using google script only ?

I welcome any help or suggestion how can I create text as showed in Pic. 1. Than you for any help.

Upvotes: 0

Views: 837

Answers (1)

Serge insas
Serge insas

Reputation: 46802

There is actually an issue (3211) that prevents doing exactly what you want : the merge() method is causing the doc crash.

so the code below will result in something like this :

enter image description here

because we cannot merge the 2 cells in the first row from the script.

Doing it manually works well :

enter image description here


code :

function myFunction() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  body.clear();
  var table = body.appendTable([['',''],['','']]);
  table.setBorderColor('#ffffff');
  table.getCell(0,0).appendParagraph('Some important text important text important text important text ');
  //table.getCell(0,1).merge();// this is the issue...it crashes the doc
  table.getCell(1,0).appendParagraph('left text').setAlignment(DocumentApp.HorizontalAlignment.LEFT);                         
  table.getCell(1,1).appendParagraph('right text').setAlignment(DocumentApp.HorizontalAlignment.RIGHT);                             
  doc.saveAndClose()
}

Upvotes: 1

Related Questions