Questa pagina descrive come eseguire le seguenti attività relative ai moduli:
- Pubblica il modulo in modo che chi risponde possa accedervi
- Trovare un utente che risponda al modulo
- Condividere il modulo con più utenti
- Rimuovere gli utenti che hanno risposto dal modulo
- Verificare se il modulo accetta le risposte da "Chiunque abbia il link"
- Chiudere un modulo
- Annullare la pubblicazione di un modulo
- Interrompere l'accettazione di risposte a un modulo
- Verificare se un modulo è precedente
Prima di iniziare
Esegui le seguenti attività prima di procedere con quelle in questa pagina:
Recupera l'ID modulo. L'ID modulo viene restituito nel campo
formId
della risposta quando crei un modulo utilizzandoforms.create
.
Pubblica il modulo in modo che chi risponde possa accedervi
Puoi pubblicare un modulo esistente con il metodo
forms.setPublishSettings
.
Chiama il metodo
forms.setPublishSettings
con l'ID modulo.
REST
Corpo della richiesta di esempio
{
"publishSettings": {
"isPublished": true,
"isAcceptingResponses": true
}
}
Apps Script
/**
* Publishes a Google Form using its URL.
*/
function publishMyForm() {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// Publish the form. This also enables accepting responses.
form.setPublished(true);
Logger.log(`Form "${form.getTitle()}" published successfully.`);
// Optional: Verify the state
if (form.isPublished()) {
Logger.log('Form is now published.');
}
if (form.isAcceptingResponses()) {
Logger.log('Form is now accepting responses.')
}
} catch (error) {
Logger.log(`Error publishing form: ${error}`);
}
}
Python
Node.js
Trovare chi ha risposto al modulo
Puoi recuperare un elenco di tutti gli utenti che hanno accesso come utenti che rispondono (ruolo PUBLISHED_READER) utilizzando Form.getPublishedReaders(). Questo restituisce un array di oggetti utente.
REST
Aggiungi il parametro di query includePermissionsForView=published
all'URL della richiesta.
Apps Script
/**
* Gets and logs the email addresses of all responders for a form.
*/
function listResponders() {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// Get the array of User objects representing responders
const responders = form.getPublishedReaders();
// Log the responders
Logger.log("Following can respond to the form");
responders.forEach(responder => Logger.log(responder.getEmail()));
return responders;
} catch (error) {
Logger.log(`Error getting responders: ${error}`);
}
}
Python
Node.js
Condividere il modulo con più utenti
Per aggiungere gli utenti che possono aprire e rispondere a un modulo, puoi utilizzare il metodo permissions.create
di Drive.
Chiama il metodo
permissions.create
con l'ID modulo e le impostazioni di accesso.
REST
Corpo della richiesta di esempio
{
"view": "published",
"role": "reader",
"type": "user",
"emailAddress": "user@example.com"
}
Apps Script
/**
* Adds a single responder to a form using their email address.
*/
function `addSingleResponderByEmail()` {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
// Replace with the responder's email address
const responderEmail = 'responder@example.com';
try {
const form = FormApp.openByUrl(formUrl);
// Add the user as a responder
form.addPublishedReader(responderEmail);
Logger.log(`Added ${responderEmail} as a responder to form "${
form.getTitle()}".`);
} catch (error) {
Logger.log(`Error adding responder: ${error}`);
}
}
Python
Node.js
Rimuovere gli utenti che hanno risposto dal modulo
Puoi rimuovere i singoli utenti che hanno risposto in base al loro indirizzo email o utilizzando un oggetto User. La rimozione di un rispondente comporta la revoca della sua capacità di visualizzare e inviare il modulo, a meno che non abbia accesso tramite altri mezzi, come la condivisione del dominio o l'accesso al Drive condiviso.
Rimuovere un singolo rispondente tramite indirizzo email
REST
DELETE https://www.googleapis.com/drive/v3/files/{fileId}/permissions/PERMISSION
permissionID può essere trovato "elencando i responsabili" come descritto in precedenza
Apps Script
/**
* Removes a single responder from a form using their email address.
*/
function `removeSingleResponderByEmail()` {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
// Replace with the responder's email address to remove
const responderEmailToRemove = 'responder-to-remove@example.com';
try {
const form = FormApp.openByUrl(formUrl);
// Remove the user as a responder
form.removePublishedReader(responderEmailToRemove);
Logger.log(`Removed ${responderEmailToRemove} as a responder from form "${
form.getTitle()}".`);
} catch (error) {
Logger.log(`Error removing responder: ${error}`);
}
}
Python
Node.js
Controlla se il modulo accetta le risposte da "Chiunque abbia il link"
Per verificare se il modulo accetta le risposte di chiunque abbia un link, devi attivare il servizio avanzato di Drive.
- Attiva il servizio Drive avanzato:
- Apri il progetto Apps Script.
- Fai clic su Servizi (l'icona Più accanto a Servizio).
- Individua l'API Drive e fai clic su Aggiungi.
- Fai clic su Aggiungi.
Apps Script
function `isAnyoneWithLinkResponder`(formId) {
let permissions = Drive.Permissions.list(formId, { includePermissionsForView: 'published' }).permissions;
if (permissions) {
for (const permission of permissions) {
if (permission.type === 'anyone' && permission.view === 'published' && permission.role === 'reader') {
return true;
}
}
}
return false;
}
Python
Node.js
Per impostare l'opzione "Chiunque abbia il link" può rispondere al modulo:
Apps Script
function `setAnyoneWithLinkResponder`(formId) {
Drive.Permissions.create({
type: 'anyone',
view: 'published',
role: 'reader',
}, formId);
}
Python
Node.js
Per rimuovere l'opzione "Chiunque abbia il link" che consente di rispondere al modulo:
Apps Script
function `removeAnyoneWithLinkResponder`(formId) {
let permissions = Drive.Permissions.list(formId, { includePermissionsForView: 'published' }).permissions;
if (permissions) {
for (const permission of permissions) {
if (permission.type === 'anyone' && permission.role === 'reader') {
Drive.Permissions.remove(formId, permission.id);
}
}
}
}
Python
Node.js
Chiudere un modulo
Per annullare la pubblicazione di un modulo, utilizza il metodo Forms.setPublished(false). {/apps-script/reference/forms/form#setpublishedenabled} La mancata pubblicazione di un modulo lo rende non disponibile e interrompe automaticamente l'accettazione delle risposte.
REST
Corpo della richiesta di esempio
POST https://forms.googleapis.com/v1/forms/{formId}:setPublishSettings
{
"publishSettings": {
"publishState": {
"isPublished": false
}
}
}
Apps Script
/**
* Unpublishes a Google Form using its URL.
*/
function unpublishMyForm() {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// Unpublish the form. This also disables accepting responses.
form.setPublished(false);
Logger.log(`Form "${form.getTitle()}" unpublished successfully.`);
// Optional: Verify the state
if (!form.isPublished()) {
Logger.log('Form is now unpublished.');
}
if (!form.isAcceptingResponses()) {
Logger.log('Form is no longer accepting responses.');
}
} catch (error) {
Logger.log(`Error unpublishing form: ${error}`);
}
}
Python
Node.js
Per interrompere l'accettazione delle risposte per un modulo senza annullarne la pubblicazione, puoi utilizzare il metodo Form.setAcceptingResponses(false)
.
Chi risponde al modulo vedrà la pagina e il messaggio del modulo chiuso.
REST
Corpo della richiesta di esempio
POST https://forms.googleapis.com/v1/forms/{formId}:setPublishSettings
{
"publishSettings": {
"publishState": {
"isPublished": true,
"isAcceptingResponses": false
}
}
}
Apps Script
/**
* Stop a Google Form from accepting responses using its URL.
*/
function closeMyFormForAcceptingResponses() {
// Replace with the URL of your Google Form
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// This disables the form for accepting responses.
form.setAcceptingResponses(false);
Logger.log(`Form "${form.getTitle()}" closed for accepting responses successfully.`);
// Optional: Verify the state
if (form.isPublished()) {
Logger.log('Form is still published.');
}
if (!form.isAcceptingResponses()) {
Logger.log('Form is no longer accepting responses.');
}
} catch (error) {
Logger.log(`Error unpublishing form: ${error}`);
}
}
Python
Node.js
Verificare se un modulo è precedente
I moduli legacy sono moduli che non dispongono del campo publishSettings, mentre tutti i nuovi moduli creati supportano le impostazioni di pubblicazione.
Controlla se un modulo è precedente o meno determinando se supporta la pubblicazione. Questo metodo viene utilizzato per determinare se i metodi setPublished(enabled)
e isPublished()
e le autorizzazioni per i rispondenti sono abilitati.
Apps Script
/**
* Checks if a form supports advanced responder permissions (i.e., is not a legacy form).
*/
function `checkIfFormSupportsPublishing()` {
// TODO(developer): Replace the URL with your own.
const formUrl = 'https://docs.google.com/forms/d/YOUR_FORM_ID/edit';
try {
const form = FormApp.openByUrl(formUrl);
// Checks whether the form supports publishing or not and logs it to the console.
const supportsPublishing = form.supportsAdvancedResponderPermissions();
if (supportsPublishing) {
Logger.log(`Form "${form.getTitle()}" supports publishing (not a legacy
form).`);
} else {
Logger.log(`Form "${form.getTitle()}" is a legacy form (does not support
publishing).`);
}
return supportsPublishing;
} catch (error) {
Logger.log(`Error unpublishing form: ${error}`);
}
}
Python
Node.js