title: Cleaning up a Google App Script for updating Forms
body: <p>I have a script that we use to update dropdown options on more than one Google Form. I am NOT a developer or anything even close but thanks to years of trial and error I can usually get what I want out of a script.</p>
  <p>Below is the current script that we use, but I am wondering if there is a cleaner way of writing this script that does not involve that giant chunks of repeated code. I fear if we continue to add more files to this to update it will take a lot more resources and time to run.</p>
  <p>Thanks for any advice!</p>
  <p>PS this is a working script and I have changed the Form_ID's to a generic name.</p>
  <pre><code>  const GOOGLE_SHEET_NAME = &quot;FormData&quot;;
    const GOOGLE_FORM_ID = &quot;Google Form ID&quot;;
  
    const ss = SpreadsheetApp.getActiveSpreadsheet();
  
    const [header, ...data] = ss
      .getSheetByName(GOOGLE_SHEET_NAME)
      .getDataRange()
      .getDisplayValues();
  
    const choices = {};
    header.forEach((title, i) =&gt; {
      choices[title] = data.map((d) =&gt; d[i]).filter((e) =&gt; e);
    });
  
    FormApp.openById(GOOGLE_FORM_ID)
      .getItems()
      .map((item) =&gt; ({
        item,
        values: choices[item.getTitle()],
      }))
      .filter(({ values }) =&gt; values)
      .forEach(({ item, values }) =&gt; {
        switch (item.getType()) {
          case FormApp.ItemType.CHECKBOX:
            item.asCheckboxItem().setChoiceValues(values);
            break;
          case FormApp.ItemType.LIST:
            item.asListItem().setChoiceValues(values);
            break;
          case FormApp.ItemType.MULTIPLE_CHOICE:
            item.asMultipleChoiceItem().setChoiceValues(values);
            break;
          default:
          // ignore item
        }
      });
    ss.toast(&quot;Google Form Updated !!&quot;);
  };
  
  {
    const GOOGLE_SHEET_NAME = &quot;FormData&quot;;
    const GOOGLE_FORM_ID = &quot;Google Form ID2&quot;;
  
    const ss = SpreadsheetApp.getActiveSpreadsheet();
  
    const [header, ...data] = ss
      .getSheetByName(GOOGLE_SHEET_NAME)
      .getDataRange()
      .getDisplayValues();
  
    const choices = {};
    header.forEach((title, i) =&gt; {
      choices[title] = data.map((d) =&gt; d[i]).filter((e) =&gt; e);
    });
  
    FormApp.openById(GOOGLE_FORM_ID)
      .getItems()
      .map((item) =&gt; ({
        item,
        values: choices[item.getTitle()],
      }))
      .filter(({ values }) =&gt; values)
      .forEach(({ item, values }) =&gt; {
        switch (item.getType()) {
          case FormApp.ItemType.CHECKBOX:
            item.asCheckboxItem().setChoiceValues(values);
            break;
          case FormApp.ItemType.LIST:
            item.asListItem().setChoiceValues(values);
            break;
          case FormApp.ItemType.MULTIPLE_CHOICE:
            item.asMultipleChoiceItem().setChoiceValues(values);
            break;
          default:
          // ignore item
        }
      });
    ss.toast(&quot;Google Form Updated !!&quot;);
  }```
  </code></pre>