Q:

Add a Google Form question to a specific section of a form using Google Apps Script

Apologies if this has been previously answered but I have researched extensively and have come up short.
I am trying to add a number of questions to various sections of a Google Form that already exists.
I know how to add a new question,
form.addTextItem();

but this appends it to the end of the form.
I also know how to get a list of sections,
var sections = form.getItems(FormApp.ItemType.PAGE_BREAK);
var section = sections[0];

But I have been unable to find a way to add a question to a specific section, like for example,
section.addTextItem();

I'm not sure if this is just impossible or if I am missing something very simple but I am surprised that no one else has had this issue before now.
Any help would be much appreciated
Kind Regards,

A:

You want to add new item to the specific session.

Unfortunately, there are no methods for directly adding new item to the specific session. So how about this workaround? In this workaround, the following flow is used.

Input the index of session you want to add new item.
Create new item.

In this stage, the item is added to the last index of the last session.

Retrieve all sessions.
Retrieve the index of sessions.

Boundary of the session can be known as PAGE_BREAK.

Move the created item to the inputted session index using the moveItem method.

By this flow, adding new item to the specific session can be achieved.
Sample script:
var sessionIndex = 1; // Please set the index you want to insert.

var form = FormApp.getActiveForm();

var newItem = form.addTextItem().setTitle("New sample item").getIndex(); // New sample item

var items = form.getItems(FormApp.ItemType.PAGE_BREAK);
var sessions = [0];
for (var i = 0; i < items.length; i++) {
  sessions.push(items[i].getIndex());
}
var insertIndex = sessions[sessionIndex + 1] || null;
if (insertIndex) {
  form.moveItem(newItem, insertIndex);
}

Note:

In this sample script, the start of sessionIndex is 0.

For example, when sessionIndex is 1, the created item is added to the 2nd session.

This is a sample script. So please modify it for your situation.

References:

Enum ItemType
moveItem(from, to)

If this was not the result you want, I apologize.

