2

I've created a script which, when a form is submitted, is triggered to create a document and replace several tags with the information from the answers.

As part of this, I create a variable which stores several answers with line breaks between them, e.g.:

if (marketingConditions.includes("Option 1")) {
  marketingConditionsReplacement = marketingConditionsReplacement + '\n' + "List item 1";
}
if (marketingConditions.includes("Option 2")) {
  marketingConditionsReplacement = marketingConditionsReplacement + '\n' + "List item 2";
}
if (marketingConditions.includes("Option 3")) {
  marketingConditionsReplacement = marketingConditionsReplacement + '\n' + " List item 3";
}

I then have a bullet point in my document which has a reference and is replaced with:

body.replaceText("{{MarketingConditions}}", marketingConditionsReplacement);

However, the new line (as expected) does not append each of these string elements as new list items.

I want it to create this

  • List item 1
  • List item 2
  • List item 3

if all of the conditions are true, for example.

I'm wondering if it's better to do this with an array instead, where each of them is appended as a separate item, but I'm not sure how I could use 'appendListItem' with this.

Any suggestions would be very much appreciated as I'm very new to App Scripts!

1 Answer 1

2

I'd prefer to use an array indeed.

Suppose you have the doc template like this:

enter image description here

You can replace the list by elements of your array this way:

function main() {
  var list_items = ['List item 1', 'List item 2', 'List item 3']; // the array
  
  var body = DocumentApp.getActiveDocument().getBody();

  var list = body.getListItems()[0];      // get the template list
  var atts = list.getAttributes();        // get the list attributes
  var indent = list.getIndentFirstLine(); // get the list indent

  list.setText(list_items[0]);            // replace 1st line of the list
  
  var index = body.getChildIndex(list);   // get number of paragraph that contains the list


  // append elements of the array to the list

  for (var i=1; i<list_items.length; i++) {
    var new_list_item = body.insertListItem(index+i, list_items[i]);
    new_list_item.setAttributes(atts);
    new_list_item.setIndentFirstLine(indent);
  }
}

Output will look like this:

enter image description here

References:

https://developers.google.com/apps-script/reference/document/list-item

https://developers.google.com/apps-script/reference/document/glyph-type

Sign up to request clarification or add additional context in comments.

3 Comments

What if the 'Template list' is the last list item in one of a number of separate lists in the document. Is there any way to find that specific place to start adding list items from the array?
var all_lists = body.getListItems() gets you all lists in the doc. var list = body.getListItems()[all_lists.length-1] gets you a last list in the doc. And actually you can put in the template list some unique phrase and find the list whenever it's placed in the doc. Here is my example how you can find the table row that contains the word 'Derek' stackoverflow.com/a/68764572/14265469
I've rephrased this question here: stackoverflow.com/questions/68774323/… - I don't think I explained it well here, but I appreciate your time on this so far!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.