Skip to content

Instantly share code, notes, and snippets.

@Ellpeck
Last active November 9, 2021 21:37
Show Gist options
  • Save Ellpeck/524a0ace19d56e9cfb07cd1c6990e3ea to your computer and use it in GitHub Desktop.
Save Ellpeck/524a0ace19d56e9cfb07cd1c6990e3ea to your computer and use it in GitHub Desktop.
A simple script for Google Apps Script that automatically creates a new card in Nextcloud Deck 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 board editor
const username = "MyUser";
// password (or app password if 2FA is enabled) of board editor
const password = "Pa55w0rd";
// the board that form responses should be added to
const board = "Board Name";
// the list on the board that form responses should be added to
const list = "List Name";
// name of the form field that should be used as the title of the created cards (optional)
const titleField = "";
function onFormSubmit(event) {
let deck = `${cloud}/index.php/apps/deck/api/v1.0`;
let headers = {
"OCS-APIRequest": "true",
"Authorization": `Basic ${Utilities.base64Encode(`${username}:${password}`)}`
};
// get the board id
let boards = UrlFetchApp.fetch(`${deck}/boards`, {
"headers": headers
});
let boardId = JSON.parse(boards).find(e => e.title == board)?.id;
if (!boardId)
throw `Can't find board with name ${board}`;
console.log(`Board id is ${boardId}`);
// get the stack id
let stacks = UrlFetchApp.fetch(`${deck}/boards/${boardId}/stacks`, {
"headers": headers
});
let stackId = JSON.parse(stacks).find(e => e.title == list)?.id;
if (!stackId)
throw `Can't find list with name ${list}`;
console.log(`List id is ${stackId}`);
// create card 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`;
}
console.log(`Creating card "${title}" with the following content:\n${content}`);
// create the new card
UrlFetchApp.fetch(`${deck}/boards/${boardId}/stacks/${stackId}/cards`, {
"headers": headers,
"method": "post",
"payload": {
"title": title,
"type": "plain",
"order": 0,
"description": content
}
});
console.log("Success");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment