Google Script - Insert indents to appended texts

I'm trying to pass information from a google spreadsheet to a google doc. This is the view of the table: enter image description here

The first column only shows once and when it's list of items for that category will be shown.

I managed to present this information in a table inside the doc but it's required to show it as text since the minimum height of each table row is still too much.

This information will travel as an array with two columns and I'll iterate thru it. This is what I've tried:

  //servicios_numrows_Q  -> number of rows for the data
  //value_tabla_servicios -> variable that contains the information

  for(var t = 0; t < servicios_numrows_Q - 1; t++){
    if(body.appendParagraph(value_tabla_servicios[t+1][0]) == ''){ //Am I in a row that hasn't a Category Title?
      "\t" + body.appendParagraph(value_tabla_servicios[t+1][1]);
    }else
    {
      body.appendParagraph(value_tabla_servicios[t+1][0]) + "\t" 
      + body.appendParagraph(value_tabla_servicios[t+1][1]);
    }
  }

However, adding a tab only results in each element being shown in a separate line.

enter image description here

Is there a way to show the first and second column in the same line? Adding spaces instead of tab?

Upvotes: 4

Views: 399

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to convert the top image (Spreadsheet) of your question as the paragraphs of Google Document using Google Apps Script.

If my understanding is correct, how about this answer?

Modification points:

  • When you add \t to the top of text using appendParagraph, please use as follows.
    • "\t" + body.appendParagraph(value_tabla_servicios[t+1][1]); is modified to body.appendParagraph("\t" + value_tabla_servicios[t+1][1]);.
    • body.appendParagraph(value_tabla_servicios[t+1][0]) + "\t" + body.appendParagraph(value_tabla_servicios[t+1][1]); is modified to body.appendParagraph(value_tabla_servicios[t+1][0] + "\t" + value_tabla_servicios[t+1][1]);
  • In your script, when value_tabla_servicios[t+1] is declared as a variable, the readability of script can be increased.
  • If you want to align the column "B", "\t\t" might be suitable instead of "\t".

When above points are reflected to your script, it becomes as follows. Please think of this as just one of several answers.

Modified script:

for(var t = 0; t < servicios_numrows_Q - 1; t++){
  var row = value_tabla_servicios[t+1];  // Added
  if (row[0] == ''){  // Modified
    body.appendParagraph("\t\t" + row[1]);  // Modified
  } else {
    body.appendParagraph(row[0] + "\t" + row[1]);  // Modified
  }
}
  • By the length of value of row[0], "\t\t\t" of body.appendParagraph("\t\t" + row[1]) might be suitable. About this, please check your actual situation.

Reference:

If I misunderstood your question and this was not the direction you want, I apologize. If the error occurs, can you provide a sample Spreadsheet, output you want and your whole script? By this, I would like to confirm it. Of course, please remove your personal information.

Upvotes: 3

Related Questions