0
votes

I am using code to read information from Google Sheets (results from Google Forms) with C# but now I want to update a dropdown list for a specific Google forms with C#. I have seen code where you can update a dropdown list with google app script (https://developers.google.com/apps-script/reference/forms )but I can not find code to do this within C#. It is possible within C# to update a google sheet so you should think it is possible to do the same with Google Forms. Does anyone done this? The example i have found is very simple within app script

var existingForm = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
var item = form.getitembyid(3567489);
var values = ["oranges", "Tomatoes"]
item.asListItem().setChoicesValues(values);
1

1 Answers

1
votes

There is no Forms API, so you cannot do this directly with C#. Because of this, the only option, if you want to do this programmatically, is using Apps Script.

There are workarounds, though, to "wrap" the Apps Script, and call it remotely from your C# program:

Workaround #1 (Apps Script Web App):

You could publish your script as a web app and use this as a URL endpoint that you will access with your C# application. The web app would receive the HTTP requests from your application, and edit the form. For example, you could do a POST request with data about the items you want to edit, and use the corresponding event object to retrieve that information. It could be something along these lines:

function doPost(e) {
  var formId = e.parameter.id; // Your form id
  var itemId = e.parameter.itemId; // Your item id
  var form = FormApp.openById(formId);
  var item = form.getItemById(itemId);
  var values = e.parameters.values; // ["oranges", "Tomatoes"]
  item.asListItem().setChoicesValues(values);
}

Workaround #2 (Apps Script API):

Another way, if you want to edit your form remotely from your C# application, is to use Apps Script API to create and run the Apps Script project that contains the code for editing the form. Beware, this is not an easy way, and it would take several steps:

  1. Get access to the Form via OAuth 2.0.
  2. Using this library, create a project with Apps Script API, by using the method projects.create. I'd suggest you to create a project that is bound to the Form, so that you don't need to authorize the script when it's time to execute it. If you do that, you would need to specify the parentId in your request body (and in your script you could use FormApp.getActiveForm() instead, which wouldn't require authorization).
  3. Using the scriptId retrieved from the response in previous step, call projects.updateContent, in which you would have to add the code corresponding to the files in your project.
  4. Call scripts.run to execute your code an update your form. If you have created a standalone script in step 2, you will have to authorize your project via OAuth 2.0.

Reference: