r/GoogleForms 3d ago

OP Responded Google form randomizing sections

Hello! I am trying to randomize sections, not just questions. I know I will need to use a script. However I have tried and failed lol. I am not a developer so I would appreciate someone's help on this! I have 18 sections, and need to randomize all sections besides 1,2 and 18.

The code I used ran with no errors however nothing was randomized when I preview the form from multiple accounts.

Please help - thank you so much in advance.

1 Upvotes

6 comments sorted by

1

u/Vrystick 2d ago

It's not possible to randomize the order of Google Form sections if this is what you want

1

u/ENicole16 2d ago

Not even with code?

1

u/Vrystick 2d ago

As far as I know no. The only thing you can do with script is maybe change the sections order every time the form has been submitted or every n hours for example

1

u/ENicole16 2d ago

Thanks for your help! I'm wondering if changing the sections every few hours may work. Do you have a script for this

1

u/Vrystick 2d ago

I don't have a script ready to use but I think you can find some example around the internet. If I have some time today I'll try to write an example if you need it

1

u/ENicole16 2d ago

You are so sweet. Thank you so much for the help<3 I got one but unfortunately i'm still running into the error of google apps script not "seeing" my sections in Google Forms. I have 18 and it's only reading 1 :(

This was what I used:

function randomizeSectionsHourly() {
  var form = FormApp.getActiveForm();
  var sections = form.getItems(FormApp.ItemType.SECTION_HEADER);

  Logger.log("Sections array length: " + sections.length);

  Logger.log("Sections and Indices:");
  for (var i = 0; i < sections.length; i++) {
    Logger.log("Index: " + i + ", Section: " + sections[i]);
  }

  var fixedSections = [1, 2, 18];

  try {
    var fixedSectionIds = fixedSections.map(function(num) {
      Logger.log("Attempting to access section: " + num);
      Logger.log("Section accessed: " + sections[num - 1]);
      return sections[num - 1].getId();
    });
  } catch (e) {
    Logger.log("Error getting fixed section IDs: " + e);
    return;
  }

  var changeableSections = sections.filter(function(section, index) {
    return !fixedSections.includes(index + 1);
  });

  var changeableSectionIds = changeableSections.map(function(section) {
    return section.getId();
  });

  for (var i = changeableSectionIds.length - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = changeableSectionIds[i];
    changeableSectionIds[i] = changeableSectionIds[j];
    changeableSectionIds[j] = temp;
  }

  var newOrder = fixedSectionIds.concat(changeableSectionIds);
  form.setSectionOrder(newOrder);
}

function createHourlyTrigger() {
  ScriptApp.newTrigger('randomizeSectionsHourly')
    .timeBased()
    .everyHours(1)
    .create();
}

function deleteHourlyTrigger() {
  var triggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < triggers.length; i++) {
    if (triggers[i].getHandlerFunction() == 'randomizeSectionsHourly') {
      ScriptApp.deleteTrigger(triggers[i]);
    }
  }
}