fromapiclientimportdiscoveryfromhttplib2importHttpfromoauth2clientimportclient,file,toolsSCOPES="https://www.googleapis.com/auth/drive"DISCOVERY_DOC="https://forms.googleapis.com/$discovery/rest?version=v1"store=file.Storage("token.json")creds=Noneifnotcredsorcreds.invalid:flow=client.flow_from_clientsecrets("client_secrets.json",SCOPES)creds=tools.run_flow(flow,store)form_service=discovery.build("forms","v1",http=creds.authorize(Http()),discoveryServiceUrl=DISCOVERY_DOC,static_discovery=False,)form={"info":{"title":"My new form",},}# Prints the details of the sample formresult=form_service.forms().create(body=form).execute()print(result)
'use strict';constpath=require('path');constgoogle=require('@googleapis/forms');const{authenticate}=require('@google-cloud/local-auth');asyncfunctionrunSample(query){constauthClient=awaitauthenticate({keyfilePath:path.join(__dirname,'credentials.json'),scopes:'https://www.googleapis.com/auth/drive',});constforms=google.forms({version:'v1',auth:authClient,});constnewForm={info:{title:'Creating a new form in Node',},};constres=awaitforms.forms.create({requestBody:newForm,});console.log(res.data);returnres.data;}if(module===require.main){runSample().catch(console.error);}module.exports=runSample;
复制现有表单
您可以使用 Google Drive API 复制现有表单,以便更轻松地重复使用内容。您可以在 Google 表单网址中找到表单 ID:
importos.pathfromgoogle.auth.transport.requestsimportRequestfromgoogle.oauth2.credentialsimportCredentialsfromgoogle_auth_oauthlib.flowimportInstalledAppFlowfromgoogleapiclient.discoveryimportbuild# If modifying these scopes, delete the file token.json.SCOPES=["https://www.googleapis.com/auth/drive"]defmain():"""Shows copy file example in Drive v3 API. Prints the name, id and other data of the copied file. """creds=Noneifos.path.exists("token.json"):creds=Credentials.from_authorized_user_file("token.json",SCOPES)# If there are no (valid) credentials available, let the user log in.ifnotcredsornotcreds.valid:ifcredsandcreds.expiredandcreds.refresh_token:creds.refresh(Request())else:flow=InstalledAppFlow.from_client_secrets_file("client_secrets.json",SCOPES)creds=flow.run_local_server(port=0)# Save the credentials for the next runwithopen("token.json","w")astoken:token.write(creds.to_json())service=build("drive","v3",credentials=creds)# Call the Drive v3 APIorigin_file_id="1ox-6vHFeKpC6mon-tL5ygBC8zpbTnTp76JCZdIg80hA"# example IDcopied_file={"title":"my_copy"}results=(service.files().copy(fileId=origin_file_id,body=copied_file).execute())print(results)if__name__=="__main__":main()
fromapiclientimportdiscoveryfromhttplib2importHttpfromoauth2clientimportclient,file,toolsSCOPES="https://www.googleapis.com/auth/forms.body"DISCOVERY_DOC="https://forms.googleapis.com/$discovery/rest?version=v1"store=file.Storage("token.json")creds=Noneifnotcredsorcreds.invalid:flow=client.flow_from_clientsecrets("client_secrets.json",SCOPES)creds=tools.run_flow(flow,store)form_service=discovery.build("forms","v1",http=creds.authorize(Http()),discoveryServiceUrl=DISCOVERY_DOC,static_discovery=False,)form={"info":{"title":"My new quiz",}}# Creates the initial formresult=form_service.forms().create(body=form).execute()# JSON to convert the form into a quizupdate={"requests":[{"updateSettings":{"settings":{"quizSettings":{"isQuiz":True}},"updateMask":"quizSettings.isQuiz",}}]}# Converts the form into a quizquestion_setting=(form_service.forms().batchUpdate(formId=result["formId"],body=update).execute())# Print the result to see it's now a quizgetresult=form_service.forms().get(formId=result["formId"]).execute()print(getresult)
'use strict';constpath=require('path');constgoogle=require('@googleapis/forms');const{authenticate}=require('@google-cloud/local-auth');asyncfunctionrunSample(query){constauthClient=awaitauthenticate({keyfilePath:path.join(__dirname,'credentials.json'),scopes:'https://www.googleapis.com/auth/drive',});constforms=google.forms({version:'v1',auth:authClient,});constnewForm={info:{title:'Creating a new form for batchUpdate in Node',},};constcreateResponse=awaitforms.forms.create({requestBody:newForm,});console.log('New formId was: '+createResponse.data.formId);// Request body to convert form to a quizconstupdateRequest={requests:[{updateSettings:{settings:{quizSettings:{isQuiz:true,},},updateMask:'quizSettings.isQuiz',},},],};constres=awaitforms.forms.batchUpdate({formId:createResponse.data.formId,requestBody:updateRequest,});console.log(res.data);returnres.data;}if(module===require.main){runSample().catch(console.error);}module.exports=runSample;
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-04-09。"],[],["This document details how to create, duplicate, and convert forms. To create a new form, use the `forms.create()` method with a title. To duplicate a form, employ the Drive API's `files.copy()` method, specifying the form's ID. To convert a form into a quiz, use the `batch.update()` method to change the `isQuiz` setting to true, also requires the form ID. Authorization and authentication setup as indicated in the instructions is required.\n"]]