DEV Community

Matheus Coelho
Matheus Coelho

Posted on

Automação para formulários Google

PT-BR

Me pediram para criar um formulário inteligente, onde ele atualizasse um item em específico de acordo com o que o usuário inserisse. Como uma espécie de ID.

Através do AppsScript, criei o seguinte código abaixo para realizar isso:

function updateForms() {
    const id = "Enter your SpreadSheet ID here!";
    const sheetName = "Enter the Sheet's name of items want to be auto uploads!";

    const ss = SpreadsheetApp.openById(id);
    const sheet = ss.getSheetByName(sheetName);
    const range = sheet.getDataRange().getValues(); // i recommend you do a sheet only to set data'll be upload

    const choiceValues = [...new Set(range.map(row => row[0]).filter(value => value))];

    const form = FormApp.openById("Enter your Forms ID here!");
    const items = form.getItems();

    for (var i in items) {
      if (items[i].getTitle() == "Set here the question name of Forms!") {
        items[i].asListItem().setChoiceValues(choiceValues);
        return;
      }
    }
    Logger.log("Nothing found...");
  }
Enter fullscreen mode Exit fullscreen mode

O código era para atualizar os dados que alimentariam um relatório semanal.

Encontre mais informações no repositório.


EN-US

I was asked to create a smart form that would update a specific item based on what the user entered. Like a sort of ID.

Using AppsScript, I created the following code to do this:

function updateForms() {
    const id = "Enter your SpreadSheet ID here!";
    const sheetName = "Enter the Sheet's name of items want to be auto uploads!";

    const ss = SpreadsheetApp.openById(id);
    const sheet = ss.getSheetByName(sheetName);
    const range = sheet.getDataRange().getValues(); // i recommend you do a sheet only to set data'll be upload

    const choiceValues = [...new Set(range.map(row => row[0]).filter(value => value))];

    const form = FormApp.openById("Enter your Forms ID here!");
    const items = form.getItems();

    for (var i in items) {
      if (items[i].getTitle() == "Set here the question name of Forms!") {
        items[i].asListItem().setChoiceValues(choiceValues);
        return;
      }
    }
    Logger.log("Nothing found...");
  }
Enter fullscreen mode Exit fullscreen mode

The code was to update the data that would feed a weekly report.

Find more information in the repository.

Top comments (0)