Skip to content

Instantly share code, notes, and snippets.

@Ellpeck
Created February 19, 2022 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ellpeck/5790c9b08b164185c1dfa90c63a4757a to your computer and use it in GitHub Desktop.
Save Ellpeck/5790c9b08b164185c1dfa90c63a4757a to your computer and use it in GitHub Desktop.
A simple script for Google Apps Script that automatically creates a new markdown file in a Nextcloud directory for each new Google Forms response
// How to use:
// 1. Open the script editor for the form ("Script Editor" in top right menu)
// 2. Create a new file, paste this code and edit the settings below
// 3. Go to Triggers and add a new trigger with the following settings:
// - Function: onFormSubmit
// - Event source: From form
// - Event type: On form submit
// 4. Allow permissions, and you're done!
// link to root of cloud installation
const cloud = "https://cloud.example.com";
// username of the user that should be logged in
const username = "MyUser";
// password (or app password if 2FA is enabled) of the user that should be logged in
const password = "Pa55w0rd";
// directory that form responses should be added to
const directory = "DirectoryName";
// name of the form field that should be used in the title of the created files (optional)
const titleField = "";
function onFormSubmit(event) {
// create file info from form
let content = "";
let title = "Form Response";
for (let response of event.response.getItemResponses()) {
let field = response.getItem().getTitle();
let value = response.getResponse().toString().replaceAll("\n", " \n");
if (field == titleField && value)
title = value;
content += `**${field}**\n${value}\n\n`;
}
title = `${new Date().toJSON().replaceAll(":", "-")} ${title}`;
console.log(`Creating file "${title}" with the following content:\n${content}`);
// create the new file
UrlFetchApp.fetch(`${cloud}/remote.php/dav/files/${username}/${directory}/${title}.md`, {
"headers": {
"OCS-APIRequest": "true",
"Authorization": `Basic ${Utilities.base64Encode(`${username}:${password}`)}`
},
"method": "put",
"payload": content
});
console.log("Success");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment