I have a giant Google Docs that looks like this:

this is a line with some text, 1
this is a line with some more text, 1
here is another line with some text, 0
here is another line with some text, 1
here is yet another line with text, 0
and finally here is a line with text, 1

The lines with text can be anywhere from a few words to a few paragraphs. However, they always end in either a 1 or 0. These lines were taken from a spreadsheet, and I can access them as text block in a cell with an adjacent column containing 1 or 0.

The Excel would look something like:

block of text | 0
block of text | 1
block of text | 0
block of text | 0
block of text | 1

The problem I face:

Everything with a `1` should be right aligned on this document.
Everything with a `0` should be left aligned on this document.

I'm hoping there is a way to do this (either my writing a function/using a tool on the spreadsheet or selecting relevant text in the document). I suspect trying to select the lines in the document may be more difficult, since some text blocks span multiple paragraphs but only have one numeric indicator.


2 Answers 2


The following Apps Script takes the values from the range A1:B4 of the currently active sheet, and uses them to fill a new document (called Test Document) in the specified way. Each entry from column A is added as a paragraph, and aligned according to column B (zero entry = left, nonzero = right).

If you want to modify an existing document, use var doc = DocumentApp.openById('DOCUMENT_ID_GOES_HERE'); instead of the first line of this function.

function alignParagraphs() {
  var doc = DocumentApp.create('Test Document');
  var body = doc.getBody();

  var range = SpreadsheetApp.getActiveSheet().getRange("A1:B4");
  var values = range.getValues();
  for (var i = 0; i < values.length; i++) {
    .setAlignment(values[i][1] ? DocumentApp.HorizontalAlignment.RIGHT : DocumentApp.HorizontalAlignment.LEFT);

Copy/Paste from Excel into Sheets (as columns A:B). Filter to select 0 in ColumnB and align left. Change filter to select 1 in ColumnB and align right. Results shown in ColumnsD:E:

WA100848 first example

Copy/Paste into Docs for result:

WA100848 second example

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.