Home > Blockchain >  Google Script Line Break on Section Header
Google Script Line Break on Section Header

Time:05-16

I am currently creating a line break for Google Script. I managed to create a line break for title and the content inside. However, when I tried to create line break for a new header title. I could not find the attribute or variable for new section title, do anyone know how to call out the variable of it?enter image description here

As the title in the front can be called using

var mainTitle = form.getTitle();

and the variable for the text can be called using

var title = questions[i].getTitle();

Do anyone know the variable of the title for a new section title?

CodePudding user response:

Building a little upon liquidkat's comment, you can check the Google Forms Apps Script documentation for this.

The form is composed of items. You can get a list by calling getItems() on the form.

  let myform = FormApp.openById("...")
  let items = myform.getItems() //an array with all the items

  for (i in items){
    console.log(items[i].getTitle()) // will list the titles of all the items
  }

To narrow down the search you can also get items by itemType. Your screenshot seems to be a PAGE_BREAK item so here's an example:

  let myform = FormApp.openById("...")
  let items = myform.getItems(FormApp.ItemType.PAGE_BREAK) //an array with only page breaks

Note that retrieving the objects as Item only has the properties shared by all item types. If you want to access properties specific to a certain item type you have to cast it to that type. Here's the sample from Google:

// Create a new form and add a text item.
var form = FormApp.create('Form Name');
form.addTextItem();

// Access the text item as a generic item.
var items = form.getItems();
var item = items[0];

// Cast the generic item to the text-item class.
if (item.getType() == 'TEXT') {
  var textItem = item.asTextItem();
  textItem.setRequired(false);
}

And as you probably noticed, the very first section that the Form starts with doesn't really count as one of the items, and you can check it with myform.getTitle().

Reference:

  • Related