Na tej stronie dowiesz się, jak wykonywać te działania związane z formularzami:
- Publikowanie formularza, aby był dostępny dla użytkowników
- Znajdowanie osób, które odpowiedziały na pytania w formularzu
- Udostępnianie formularza większej liczbie użytkowników
- Usuwanie użytkowników z formularza
- Sprawdź, czy formularz akceptuje odpowiedzi od „Każdej osoby mającej link”.
- Zamykanie formularza
- Cofanie publikacji formularza
- Zatrzymywanie akceptowania odpowiedzi w formularzu
- Sprawdzanie, czy formularz jest starszego typu
Zanim zaczniesz
Zanim wykonasz czynności opisane na tej stronie, wykonaj te zadania:
Uzyskaj identyfikator formularza. Identyfikator formularza jest zwracany w polu
formId
odpowiedzi podczas tworzenia formularza za pomocą metodyforms.create
.
Publikowanie formularza, aby był dostępny dla użytkowników
Aby opublikować istniejący formularz, użyj metody forms.setPublishSettings
.
Wywołaj metodę
forms.setPublishSettings
, podając identyfikator formularza.
REST
Przykładowa treść żądania
{
"publishSettings": {
"isPublished": true,
"isAcceptingResponses": true
}
}
Google 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
Znajdowanie użytkowników, którzy odpowiedzieli na pytania z formularza
Listę wszystkich użytkowników, którzy mają dostęp do odpowiedzi (rola PUBLISHED_READER), możesz pobrać za pomocą funkcji Form.getPublishedReaders(). Zwraca ona tablicę obiektów użytkownika.
REST
Dołącz do adresu URL żądania parametr zapytania includePermissionsForView=published
.
Google 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
Udostępnianie formularza większej liczbie użytkowników
Aby dodać osoby odpowiadające do formularza, tak aby mogły go otworzyć i na niego odpowiedzieć, możesz użyć metody Dysku permissions.create
.
Wywołaj metodę
permissions.create
z identyfikatorem formularza i ustawieniami dostępu.
REST
Przykładowa treść żądania
{
"view": "published",
"role": "reader",
"type": "user",
"emailAddress": "user@example.com"
}
Google 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
Usuwanie użytkowników z formularza
Możesz usuwać poszczególnych odpowiadających, podając ich adresy e-mail lub używając obiektu User. Usunięcie osoby odpowiadającej spowoduje odebranie jej możliwości wyświetlania i przesyłania formularza, chyba że ma ona dostęp w inny sposób, np. przez udostępnianie domeny lub dostęp do dysku współdzielonego.
Usuwanie pojedynczego respondenta za pomocą adresu e-mail
REST
DELETE https://www.googleapis.com/drive/v3/files/{fileId}/permissions/PERMISSION
Identyfikator permissionID można znaleźć, wyświetlając listę osób odpowiadających, jak opisano wcześniej.
Google 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
Sprawdź, czy formularz akceptuje odpowiedzi od „Każdej osoby mającej link”.
Aby sprawdzić, czy formularz akceptuje odpowiedzi od wszystkich osób, które mają link, musisz włączyć usługę zaawansowaną Dysku.
- Włącz usługę zaawansowaną Dysk:
- Otwórz projekt Apps Script.
- Kliknij Usługi (ikonę plusa obok opcji Usługa).
- Znajdź Drive API i kliknij Add (Dodaj).
- Kliknij Dodaj.
Google 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
Aby ustawić opcję „Każda osoba mająca link może odpowiadać na formularz”:
Google Apps Script
function `setAnyoneWithLinkResponder`(formId) {
Drive.Permissions.create({
type: 'anyone',
view: 'published',
role: 'reader',
}, formId);
}
Python
Node.js
Aby usunąć opcję „Każda osoba mająca link może odpowiadać na formularz”:
Google 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
Zamykanie formularza
Aby cofnąć publikację formularza, użyj metody Forms.setPublished(false). {/apps-script/reference/forms/form#setpublishedenabled} Cofnięcie publikacji formularza powoduje, że staje się on niedostępny i automatycznie przestaje akceptować odpowiedzi.
REST
Przykładowa treść żądania
POST https://forms.googleapis.com/v1/forms/{formId}:setPublishSettings
{
"publishSettings": {
"publishState": {
"isPublished": false
}
}
}
Google 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
Aby zatrzymać akceptowanie odpowiedzi w formularzu bez wycofywania go z publikacji, możesz użyć metody Form.setAcceptingResponses(false)
.
Osoby odpowiadające na Twój formularz zobaczą stronę zamkniętego formularza i wiadomość.
REST
Przykładowa treść żądania
POST https://forms.googleapis.com/v1/forms/{formId}:setPublishSettings
{
"publishSettings": {
"publishState": {
"isPublished": true,
"isAcceptingResponses": false
}
}
}
Google 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
Sprawdzanie, czy formularz jest starszego typu
Starsze formularze to formularze, które nie mają pola publishSettings, podczas gdy wszystkie nowo utworzone formularze obsługują ustawienia publikowania.
Sprawdź, czy formularz jest starszy, czy nie, ustalając, czy obsługuje publikowanie. Ta metoda służy do określania, czy metody setPublished(enabled)
i isPublished()
oraz uprawnienia odpowiadającego są włączone.
Google 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