Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions forms-api/demos/AppsScriptFormsAPIWebApp/Code.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

function doGet() {
return HtmlService.createTemplateFromFile('Main').evaluate();
}
280 changes: 280 additions & 0 deletions forms-api/demos/AppsScriptFormsAPIWebApp/FormsAPI.gs
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Global constants. Customize as needed.
const formsAPIUrl = 'https://forms.googleapis.com/v1beta/forms/';
const formId = '<YOUR_FORM_ID>';
const topicName = 'projects/<YOUR_TOPIC_PATH>';

// To setup pub/sub topics, see:
// https://cloud.google.com/pubsub/docs/building-pubsub-messaging-system

/**
* Forms API Method: forms.create
* POST https://forms.googleapis.com/v1beta/forms
*/
function create(title) {
const accessToken = ScriptApp.getOAuthToken();
const jsonTitle = JSON.stringify({
info: {
title: title
}
});

const options = {
'headers': {
Authorization: 'Bearer ' + accessToken
},
'method': 'post',
'contentType': 'application/json',
'payload': jsonTitle
};

Logger.log('Forms API POST options was: ' + JSON.stringify(options));
let response = UrlFetchApp.fetch(formsAPIUrl, options);
Logger.log('Response from Forms API was: ' + JSON.stringify(response));

return ('' + response);
}

/**
* Forms API Method: forms.get
* GET https://forms.googleapis.com/v1beta/forms/{formId}/responses/{responseId}
*/
function get(formId) {
const accessToken = ScriptApp.getOAuthToken();

const options = {
'headers': {
Authorization: 'Bearer ' + accessToken,
Accept: 'application/json'
},
'method': 'get'
};

try {
let response = UrlFetchApp.fetch(formsAPIUrl + formId, options);
Logger.log('Response from Forms API was: ' + response);
return ('' + response);
} catch (e) {
Logger.log(JSON.stringify(e));
return ('Error:' + JSON.stringify(e) +
'<br/><br/>Unable to find Form with formId:<br/>' + formId);
}
}

/**
* Forms API Method: forms.batchUpdate
* POST https://forms.googleapis.com/v1beta/forms/{formId}:batchUpdate
*/
function batchUpdate(formId) {
const accessToken = ScriptApp.getOAuthToken();

// Request body to add a description to a Form
const update = {
'requests': [{
'updateFormInfo': {
'info': {
'description': 'Please complete this quiz based on this week\'s readings for class.'
},
'updateMask': 'description'
}
}]
}

const options = {
'headers': {
Authorization: 'Bearer ' + accessToken
},
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(update),
'muteHttpExceptions': true,
};

let response = UrlFetchApp.fetch(formsAPIUrl + formId + ':batchUpdate',
options);
Logger.log('Response code from API: ' + response.getResponseCode());
return (response.getResponseCode());
}

/**
* Forms API Method: forms.responses.get
* GET https://forms.googleapis.com/v1beta/forms/{formId}/responses/{responseId}
*/
function responsesGet(formId, responseId) {
const accessToken = ScriptApp.getOAuthToken();

var options = {
'headers': {
Authorization: 'Bearer ' + accessToken,
Accept: 'application/json'
},
'method': 'get'
};

try {
var response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'responses/' +
responseId, options);
Logger.log('Response from Forms.responses.get was: ' + response);
return ('' + response);
} catch (e) {
Logger.log(JSON.stringify(e));
return ('Error:' + JSON.stringify(e))
}
}

/**
* Forms API Method: forms.responses.list
* GET https://forms.googleapis.com/v1beta/forms/{formId}/responses
*/
function responsesList(formId) {
const accessToken = ScriptApp.getOAuthToken();

var options = {
'headers': {
Authorization: 'Bearer ' + accessToken,
Accept: 'application/json'
},
'method': 'get'
};

try {
var response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'responses',
options);
Logger.log('Response from Forms.responses was: ' + response);
return ('' + response);
} catch (e) {
Logger.log(JSON.stringify(e));
return ('Error:' + JSON.stringify(e))
}
}

/**
* Forms API Method: forms.watches.create
* POST https://forms.googleapis.com/v1beta/forms/{formId}/watches
*/
function createWatch(formId) {
let accessToken = ScriptApp.getOAuthToken();

var myWatch = {
'watch': {
'target': {
'topic': {
'topicName': topicName
}
},
'eventType': 'RESPONSES',
}
};
Logger.log('myWatch is: ' + JSON.stringify(myWatch));

var options = {
'headers': {
Authorization: 'Bearer ' + accessToken
},
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(myWatch),
'muteHttpExceptions': false,
};
Logger.log('options are: ' + JSON.stringify(options));
Logger.log('formsAPIURL was: ' + formsAPIUrl);

var response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'watches',
options);
Logger.log(response);
return ('' + response);
}

/**
* Forms API Method: forms.watches.delete
* DELETE https://forms.googleapis.com/v1beta/forms/{formId}/watches/{watchId}
*/
function deleteWatch(formId, watchId) {
let accessToken = ScriptApp.getOAuthToken();

Logger.log('formsAPIUrl is: ' + formsAPIUrl);

var options = {
'headers': {
Authorization: 'Bearer ' + accessToken,
Accept: 'application/json'
},
'method': 'delete',
'muteHttpExceptions': false,
};

try {
var response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'watches/' +
watchId, options);
Logger.log(response);
return ('' + response);
} catch (e) {
Logger.log('API Error: ' + JSON.stringify(e));
return (JSON.stringify(e));
}

}

/**
* Forms API Method: forms.watches.list
* GET https://forms.googleapis.com/v1beta/forms/{formId}/watches
*/
function watchesList(formId) {
Logger.log('formId is: ' + formId);
let accessToken = ScriptApp.getOAuthToken();
var options = {
'headers': {
Authorization: 'Bearer ' + accessToken,
Accept: 'application/json'
},
'method': 'get'
};
try {
var response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'watches',
options);
Logger.log(response);
return ('' + response);
} catch (e) {
Logger.log('API Error: ' + JSON.stringify(e));
return (JSON.stringify(e));
}
}

/**
* Forms API Method: forms.watches.renew
* POST https://forms.googleapis.com/v1beta/forms/{formId}/watches/{watchId}:renew
*/
function renewWatch(formId, watchId) {
let accessToken = ScriptApp.getOAuthToken();

var options = {
'headers': {
Authorization: 'Bearer ' + accessToken,
Accept: 'application/json'
},
'method': 'post'
};

try {
var response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'watches/' +
watchId + ':renew', options);
Logger.log(response);
return ('' + response);
} catch (e) {
Logger.log('API Error: ' + JSON.stringify(e));
return (JSON.stringify(e));
}
}
Loading