Last active
June 12, 2023 22:54
-
-
Save lolipopshock/ecab5119d4c37c11733a05b3ba225076 to your computer and use it in GitHub Desktop.
Allow PDF embed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var scriptProperties = PropertiesService.getScriptProperties(); | |
const NOTION_API_TOKEN = scriptProperties.getProperty('NOTION_TOKEN'); | |
const NOTION_PAGE_ID = scriptProperties.getProperty('NOTION_PAGE_URL'); | |
function main(e) { | |
var itemResponses = e.response.getItemResponses(); | |
var rawResponseData = {}; | |
for (var j = 0; j < itemResponses.length; j++) { | |
var itemResponse = itemResponses[j]; | |
var question = itemResponse.getItem().getTitle(); | |
var answer = itemResponse.getResponse(); | |
var index = itemResponse.getItem().getIndex(); | |
rawResponseData[index] = { | |
"question": question, | |
"answer": answer | |
} | |
} | |
console.log(rawResponseData); | |
createNotionPage(rawResponseData); | |
} | |
function createNotionData(rawResponseData) { | |
//var children = notionChildrenTemplate(rawResponseData?.[6]?.answer || ""); | |
let hasAdvice = ! (rawResponseData?.[6]?.answer === undefined); | |
let advice = rawResponseData?.[6]?.answer || ""; | |
let statementUrl = rawResponseData[5]["answer"]; | |
let children = createChildren(statementUrl, advice, hasAdvice); | |
console.log(children); | |
// remove index 6 from rawResponseData | |
delete rawResponseData[6]; | |
var properties = Object.assign({}, ...(Object.entries(rawResponseData).map( | |
([key, value]) => { | |
return notionResponseTemplate[key](value["answer"]) | |
} | |
))) || {}; | |
console.log(properties); | |
console.log(children); | |
return [properties, children, hasAdvice]; | |
}; | |
function createChildren(statementUrl, advice, hasAdvice) { | |
let children = [ | |
{ | |
"object": "block", | |
"type": "paragraph", | |
"paragraph": { | |
"rich_text": [ | |
{ | |
"type": "text", | |
"text": { | |
"content": "Statement" | |
}, | |
"annotations": { | |
"bold": true | |
} | |
} | |
] | |
} | |
}, | |
{ | |
"object": "block", | |
"type": "pdf", | |
"pdf": { | |
"type": "external", | |
"external": { | |
"url": statementUrl | |
} | |
} | |
} | |
] | |
if (hasAdvice) { | |
children.push({ | |
"object": "block", | |
"type": "paragraph", | |
"paragraph": { | |
"rich_text": [ | |
{ | |
"type": "text", | |
"text": { | |
"content": "Additional Advice" | |
}, | |
"annotations": { | |
"bold": true | |
} | |
} | |
] | |
} | |
}) | |
children.push( | |
{ | |
"object": "block", | |
"type": "paragraph", | |
"paragraph": { | |
"rich_text": [ | |
{ | |
"type": "text", | |
"text": { | |
"content": advice | |
} | |
} | |
] | |
} | |
} | |
) | |
} | |
return children | |
} | |
const notionResponseTemplate = { | |
0: answer => { return { "Name": { "title": [{ "text": { "content": answer } }] } } }, | |
1: answer => { return { "Website": {'url': answer} } }, | |
2: answer => { return { "Institution Attended": { "select": { "name": answer } } } }, | |
3: answer => { return { "Application Year": { "select": { "name": answer } } } }, | |
4: answer => { return { "Field": { "multi_select": answer.map(field => { return { "name": field } }) } } }, | |
5: answer => { return { "Statement": {'url': answer} } }, | |
} | |
function createNotionPage(rawResponseData) { | |
const [properties, children, hasAdvice] = createNotionData(rawResponseData); | |
let payloadData = { | |
parent: { database_id: NOTION_PAGE_ID}, | |
properties: properties, | |
children: children, | |
}; | |
console.log(hasAdvice); | |
if (hasAdvice) { | |
payloadData["icon"] = { | |
"type": "emoji", | |
"emoji": "📗" | |
} | |
}; | |
let options = { | |
method: "post", | |
contentType: "application/json", | |
headers: { | |
Authorization: "Bearer " + NOTION_API_TOKEN, | |
"Notion-Version": "2021-08-16", | |
"Content-Type": "application/json", | |
}, | |
payload: JSON.stringify(payloadData), | |
}; | |
console.log(JSON.stringify(payloadData)); | |
let response = UrlFetchApp.fetch("https://api.notion.com/v1/pages", options); | |
response = JSON.parse(response.getContentText()); | |
Logger.log(response); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment