1
votes

Google Apps Script gives you the ability to get reference to a form by its id or by its URL.

https://developers.google.com/apps-script/reference/forms/form-app

I want to iterate through all forms that have a certain string in its name.

I want to avoid having to maintain a list of all the form IDs as I go along. Forms come and go, and so I'd rather find all, and iterate on them.

I've tried looking through all the docs on the Google Apps Script site, and cannot find a list method of any kind.

How can I get a list of all the forms available?

2
You can use DriveApp Service. Apps Script Documentation - get files by type Use MimeType.GOOGLE_FORMS for the type. Apps Script documentation - Mime TypeAlan Wells

2 Answers

2
votes

Or you could do it the easier way:

function allFormFilesWithStringInTitle() {
 // Log the name of every file in the user's Drive that is a form mime type
 // whose name contains "Untitled".
 //var files = DriveApp.searchFiles('title contains "Untitled"');
 var files = DriveApp.searchFiles('mimeType = "application/vnd.google-apps.form" and title contains "Untitled"');

 while (files.hasNext()) {
   var file = files.next();
   Logger.log(file.getName());
   Logger.log(file.getMimeType());
 }
}
1
votes
function goThroughAllForms() {
  var formInfo,arrayFormIDs,arrayFormNames,arrayOfIndexNumbers,
      allFormIDsWithStringInName,i,searchStr,thisID;

  searchStr = "Untitled";

  formInfo= getFilesOfType();
  arrayFormNames = formInfo[1];
  arrayOfIndexNumbers = searchFormNamesForString(arrayFormNames,searchStr);

  //Logger.log('searchStr: ' + searchStr)
  //Logger.log(arrayOfIndexNumbers)

  allFormIDsWithStringInName = [];
  arrayFormIDs = formInfo[0];

  for (i=0;i<arrayOfIndexNumbers.length;i+=1) {
    thisID = arrayFormIDs[arrayOfIndexNumbers[i]];
    allFormIDsWithStringInName.push(thisID);
  };

  Logger.log(allFormIDsWithStringInName)
};

function getFilesOfType() {
  var allFormFiles,arrFileName,arrFileID,arrFileUrls,thisFile;

  allFormFiles = DriveApp.getFilesByType(MimeType.GOOGLE_FORMS);
  arrFileName = [];
  arrFileID = [];
  arrFileUrls = [];

  while (allFormFiles.hasNext()) {
    thisFile=allFormFiles.next();
    arrFileName.push(thisFile.getName());
    arrFileID.push(thisFile.getId());
    arrFileUrls.push(thisFile.getUrl());
  };

  //Logger.log(arrFileName)
  return [arrFileID,arrFileName];
};


function searchFormNamesForString(arrayFormNames,searchStr) {
  var arrayIndexNumbers,i,L,thisName;

  arrayIndexNumbers = [];

  L = arrayFormNames.length;

  for (i=0;i<L;i+=1){
    thisName = arrayFormNames[i];
    Logger.log(thisName);
    Logger.log('thisName.indexOf(searchStr): ' + thisName.indexOf(searchStr));

    if (thisName.indexOf(searchStr) !== -1) {
      arrayIndexNumbers.push(i);
    };
  };

  return arrayIndexNumbers;
};