0

I'm wondering if there is a more efficient way to add Items to Google Forms with App Script.

My script is taking a long time and I'm wondering if there's anyway to add items more efficiently using an array or something rather than one by one. Here's the code I'm running...

function addFormItems() {
  var startTime = new Date(); var form = FormApp.create("Test") form.setIsQuiz(true)
  for (var i = 1; i < 100; i++) {
    form.addTextItem().setTitle("Question number " + i).setHelpText("Hint: answer should not be more than a couple of words")
  }
  var endTime = new Date();
  //Get runTime in seconds var runTime = (endTime - startTime)/1000;
  Logger.log("runtime is: " + runTime)
}

Currently it takes quite a long time a minute to a minute and a half (odd thing is every time I execute I get a very different runtime not sure why that happens) Any thoughts on how to speed this up is much appreciated.

I searched Documentation and couldn't find anything about adding multiple items with one call.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Sherif
  • 13
  • 3
  • 1
    Thanks Cooper. Could you please elaborate further. I know that linking the sheet will populate the question titles in the first row. I think that may help with reading the item titles but not sure how I could use that in my case were I need to add items to the form? – Sherif Jan 30 '23 at 23:24
  • In your situation, how about using Google Forms API? [Ref](https://developers.google.com/forms/api/reference/rest/v1/forms/create) – Tanaike Jan 31 '23 at 00:26
  • 2
    So... here I am looking at the comment by @Tanaike and trying to figure out how to _would/could_ I use the Forms API to create questions. And then I found [Creating Quizzes in Google Form using Google Forms API with Google Apps Script](https://gist.github.com/tanaikech/eebcb2d71eed1aab5decff64287bcc9a). And who do you think is the author of that? Yep, none other than Tanaike. – Tedinoz Jan 31 '23 at 02:43
  • @Tanaike thanks for this I wasn't aware it could be done that way! I was looking at the documentation yesterday and spinning my wheels but your code sample which Tedinoz shared is very helpful. Kudos!! Will give it a try and post back. Thanks again! – Sherif Feb 01 '23 at 02:56
  • Thank you for replying. If that was useful, I'm glad. – Tanaike Feb 01 '23 at 03:20

0 Answers0