From e973c8214347ed967c6df59efc39a233cb1e5b6f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 21 Nov 2025 22:24:12 +0000 Subject: [PATCH] fix(forms-api): resolve type errors Adds JSDoc annotations and corrects type casting to resolve TypeScript errors in the forms-api directory. - Adds @param and @return annotations to functions. - Casts the `method` property in `UrlFetchApp.fetch` options to the correct type. - Removes unhelpful try/catch blocks. - Fixes syntax errors in `retrieve_all_responses.gs`. - Uses `response.getContentText()` instead of string coercion. --- .../demos/AppsScriptFormsAPIWebApp/Code.gs | 6 +- .../AppsScriptFormsAPIWebApp/FormsAPI.gs | 239 +++++++++--------- forms-api/snippets/retrieve_all_responses.gs | 64 +++-- 3 files changed, 165 insertions(+), 144 deletions(-) diff --git a/forms-api/demos/AppsScriptFormsAPIWebApp/Code.gs b/forms-api/demos/AppsScriptFormsAPIWebApp/Code.gs index aa0a5f9e2..a9ca0fe8d 100644 --- a/forms-api/demos/AppsScriptFormsAPIWebApp/Code.gs +++ b/forms-api/demos/AppsScriptFormsAPIWebApp/Code.gs @@ -2,6 +2,7 @@ // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with 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 @@ -12,6 +13,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +/** + * @return {GoogleAppsScript.HTML.HtmlOutput} + */ function doGet() { return HtmlService.createTemplateFromFile('Main').evaluate(); -} \ No newline at end of file +} diff --git a/forms-api/demos/AppsScriptFormsAPIWebApp/FormsAPI.gs b/forms-api/demos/AppsScriptFormsAPIWebApp/FormsAPI.gs index 6e36d003c..930770b28 100644 --- a/forms-api/demos/AppsScriptFormsAPIWebApp/FormsAPI.gs +++ b/forms-api/demos/AppsScriptFormsAPIWebApp/FormsAPI.gs @@ -23,184 +23,196 @@ const topicName = 'projects/'; /** * Forms API Method: forms.create * POST https://forms.googleapis.com/v1/forms + * @param {string} title The title of the new form. + * @return {string} The form object as a JSON string. */ function create(title) { const accessToken = ScriptApp.getOAuthToken(); const jsonTitle = JSON.stringify({ info: { - title: title - } + title: title, + }, }); const options = { - 'headers': { - Authorization: 'Bearer ' + accessToken + headers: { + Authorization: 'Bearer ' + accessToken, }, - 'method': 'post', - 'contentType': 'application/json', - 'payload': jsonTitle + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('post'), + contentType: 'application/json', + payload: jsonTitle, }; console.log('Forms API POST options was: ' + JSON.stringify(options)); const response = UrlFetchApp.fetch(formsAPIUrl, options); console.log('Response from Forms API was: ' + JSON.stringify(response)); - return ('' + response); + return response.getContentText(); } /** * Forms API Method: forms.get * GET https://forms.googleapis.com/v1/forms/{formId}/responses/{responseId} + * @param {string} formId The ID of the form to get. + * @return {string} The form object as a JSON string. */ function get(formId) { const accessToken = ScriptApp.getOAuthToken(); const options = { - 'headers': { + headers: { Authorization: 'Bearer ' + accessToken, - Accept: 'application/json' + Accept: 'application/json', }, - 'method': 'get' + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('get'), }; - try { - const response = UrlFetchApp.fetch(formsAPIUrl + formId, options); - console.log('Response from Forms API was: ' + response); - return ('' + response); - } catch (e) { - console.log(JSON.stringify(e)); - return ('Error:' + JSON.stringify(e) + - '

Unable to find Form with formId:
' + formId); - } + const response = UrlFetchApp.fetch(formsAPIUrl + formId, options); + console.log('Response from Forms API was: ' + response); + return response.getContentText(); } /** * Forms API Method: forms.batchUpdate * POST https://forms.googleapis.com/v1/forms/{formId}:batchUpdate + * @param {string} formId The ID of the form to update. + * @return {number} The response code. */ 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.' + requests: [ + { + updateFormInfo: { + info: { + description: + 'Please complete this quiz based on this week\'s ' + + 'readings for class.', + }, + updateMask: 'description', }, - 'updateMask': 'description' - } - }] - } + }, + ], + }; const options = { - 'headers': { - Authorization: 'Bearer ' + accessToken + headers: { + Authorization: 'Bearer ' + accessToken, }, - 'method': 'post', - 'contentType': 'application/json', - 'payload': JSON.stringify(update), - 'muteHttpExceptions': true, + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('post'), + contentType: 'application/json', + payload: JSON.stringify(update), + muteHttpExceptions: true, }; - const response = UrlFetchApp.fetch(formsAPIUrl + formId + ':batchUpdate', - options); + const response = UrlFetchApp.fetch( + formsAPIUrl + formId + ':batchUpdate', + options, + ); console.log('Response code from API: ' + response.getResponseCode()); - return (response.getResponseCode()); + return response.getResponseCode(); } /** * Forms API Method: forms.responses.get * GET https://forms.googleapis.com/v1/forms/{formId}/responses/{responseId} + * @param {string} formId The form ID. + * @param {string} responseId The response ID. + * @return {string} The response object as a JSON string. */ function responsesGet(formId, responseId) { const accessToken = ScriptApp.getOAuthToken(); const options = { - 'headers': { + headers: { Authorization: 'Bearer ' + accessToken, - Accept: 'application/json' + Accept: 'application/json', }, - 'method': 'get' + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('get'), }; - try { - const response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'responses/' + - responseId, options); - console.log('Response from Forms.responses.get was: ' + response); - return ('' + response); - } catch (e) { - console.log(JSON.stringify(e)); - return ('Error:' + JSON.stringify(e)) - } + const response = UrlFetchApp.fetch( + formsAPIUrl + formId + '/' + 'responses/' + responseId, + options, + ); + console.log('Response from Forms.responses.get was: ' + response); + return response.getContentText(); } /** * Forms API Method: forms.responses.list * GET https://forms.googleapis.com/v1/forms/{formId}/responses + * @param {string} formId The form ID. + * @return {string} The response object as a JSON string. */ function responsesList(formId) { const accessToken = ScriptApp.getOAuthToken(); const options = { - 'headers': { + headers: { Authorization: 'Bearer ' + accessToken, - Accept: 'application/json' + Accept: 'application/json', }, - 'method': 'get' + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('get'), }; - try { - const response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'responses', - options); - console.log('Response from Forms.responses was: ' + response); - return ('' + response); - } catch (e) { - console.log(JSON.stringify(e)); - return ('Error:' + JSON.stringify(e)) - } + const response = UrlFetchApp.fetch( + formsAPIUrl + formId + '/' + 'responses', + options, + ); + console.log('Response from Forms.responses was: ' + response); + return response.getContentText(); } /** * Forms API Method: forms.watches.create * POST https://forms.googleapis.com/v1/forms/{formId}/watches + * @param {string} formId The form ID. + * @return {string} The watch object as a JSON string. */ function createWatch(formId) { const accessToken = ScriptApp.getOAuthToken(); const myWatch = { - 'watch': { - 'target': { - 'topic': { - 'topicName': topicName - } + watch: { + target: { + topic: { + topicName: topicName, + }, }, - 'eventType': 'RESPONSES', - } + eventType: 'RESPONSES', + }, }; console.log('myWatch is: ' + JSON.stringify(myWatch)); const options = { - 'headers': { - Authorization: 'Bearer ' + accessToken + headers: { + Authorization: 'Bearer ' + accessToken, }, - 'method': 'post', - 'contentType': 'application/json', - 'payload': JSON.stringify(myWatch), - 'muteHttpExceptions': false, + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('post'), + contentType: 'application/json', + payload: JSON.stringify(myWatch), + muteHttpExceptions: false, }; console.log('options are: ' + JSON.stringify(options)); console.log('formsAPIURL was: ' + formsAPIUrl); - const response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'watches', - options); + const response = UrlFetchApp.fetch( + formsAPIUrl + formId + '/' + 'watches', + options, + ); console.log(response); - return ('' + response); + return response.getContentText(); } /** * Forms API Method: forms.watches.delete * DELETE https://forms.googleapis.com/v1/forms/{formId}/watches/{watchId} + * @param {string} formId The form ID. + * @param {string} watchId The watch ID. + * @return {string} The response as a JSON string. */ function deleteWatch(formId, watchId) { const accessToken = ScriptApp.getOAuthToken(); @@ -208,73 +220,68 @@ function deleteWatch(formId, watchId) { console.log('formsAPIUrl is: ' + formsAPIUrl); const options = { - 'headers': { + headers: { Authorization: 'Bearer ' + accessToken, - Accept: 'application/json' + Accept: 'application/json', }, - 'method': 'delete', - 'muteHttpExceptions': false, + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('delete'), + muteHttpExceptions: false, }; - try { - const response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'watches/' + - watchId, options); - console.log(response); - return ('' + response); - } catch (e) { - console.log('API Error: ' + JSON.stringify(e)); - return (JSON.stringify(e)); - } - + const response = UrlFetchApp.fetch( + formsAPIUrl + formId + '/' + 'watches/' + watchId, + options, + ); + console.log(response); + return response.getContentText(); } -/** +/** * Forms API Method: forms.watches.list * GET https://forms.googleapis.com/v1/forms/{formId}/watches + * @param {string} formId The form ID. + * @return {string} The response as a JSON string. */ function watchesList(formId) { console.log('formId is: ' + formId); const accessToken = ScriptApp.getOAuthToken(); const options = { - 'headers': { + headers: { Authorization: 'Bearer ' + accessToken, - Accept: 'application/json' + Accept: 'application/json', }, - 'method': 'get' + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('get'), }; - try { - const response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'watches', - options); - console.log(response); - return ('' + response); - } catch (e) { - console.log('API Error: ' + JSON.stringify(e)); - return (JSON.stringify(e)); - } + const response = UrlFetchApp.fetch( + formsAPIUrl + formId + '/' + 'watches', + options, + ); + console.log(response); + return response.getContentText(); } /** * Forms API Method: forms.watches.renew * POST https://forms.googleapis.com/v1/forms/{formId}/watches/{watchId}:renew + * @param {string} formId The form ID. + * @param {string} watchId The watch ID. + * @return {string} The watch object as a JSON string. */ function renewWatch(formId, watchId) { const accessToken = ScriptApp.getOAuthToken(); const options = { - 'headers': { + headers: { Authorization: 'Bearer ' + accessToken, - Accept: 'application/json' + Accept: 'application/json', }, - 'method': 'post' + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('post'), }; - try { - const response = UrlFetchApp.fetch(formsAPIUrl + formId + '/' + 'watches/' + - watchId + ':renew', options); - console.log(response); - return ('' + response); - } catch (e) { - console.log('API Error: ' + JSON.stringify(e)); - return (JSON.stringify(e)); - } + const response = UrlFetchApp.fetch( + formsAPIUrl + formId + '/' + 'watches/' + watchId + ':renew', + options, + ); + console.log(response); + return response.getContentText(); } diff --git a/forms-api/snippets/retrieve_all_responses.gs b/forms-api/snippets/retrieve_all_responses.gs index c7d865e6d..5df19031d 100644 --- a/forms-api/snippets/retrieve_all_responses.gs +++ b/forms-api/snippets/retrieve_all_responses.gs @@ -1,35 +1,45 @@ -# Copyright 2022 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. +/** + * Copyright 2022 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. + */ - # [START forms_retrieve_all_responses] - function callFormsAPI() { +/** + * @fileoverview Forms API Snippet + */ +// [START forms_retrieve_all_responses] +/** + * Call the Forms API to retrieve all responses + * @see https://developers.google.com/forms/api/reference/rest/v1/forms.responses/list + */ +function callFormsAPI() { console.log('Calling the Forms API!'); const formId = ''; // Get OAuth Token - const OAuthToken = ScriptApp.getOAuthToken(); - console.log('OAuth token is: ' + OAuthToken); - const formsAPIUrl = 'https://forms.googleapis.com/v1/forms/' + formId + '/' + 'responses'; - console.log('formsAPIUrl is: ' + formsAPIUrl); - const options = { - 'headers': { + const OAuthToken = ScriptApp.getOAuthToken(); + console.log('OAuth token is: ' + OAuthToken); + const formsAPIUrl = + 'https://forms.googleapis.com/v1/forms/' + formId + '/' + 'responses'; + console.log('formsAPIUrl is: ' + formsAPIUrl); + const options = { + headers: { Authorization: 'Bearer ' + OAuthToken, - Accept: 'application/json' + Accept: 'application/json', }, - 'method': 'get' - }; -const response = UrlFetchApp.fetch(formsAPIUrl, options); - console.log('Response from forms.responses was: ' + response); + method: /** @type {GoogleAppsScript.URL_Fetch.HttpMethod} */ ('get'), + }; + const response = UrlFetchApp.fetch(formsAPIUrl, options); + console.log('Response from forms.responses was: ' + response); } -# [END forms_retrieve_all_responses] +// [END forms_retrieve_all_responses]