-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
102 lines (87 loc) · 2.84 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const uid_key = "uid" // uid key for generating the sequential registration number
const uid_prefix = "" // uid prefix
const uid_length = 4 // uid digit length
const _max_responses = 1500 // max reseponses allowed
function onOpen(e){
let ui = SpreadsheetApp.getUi()
ui.createMenu("CSR Form")
.addItem("Open form", "openWebApp")
.addItem("Reset form", "resetForm")
.addToUi()
}
/**
* insert the html file into another html file
* @param {string} filename
*/
function include(filename){
return HtmlService.createTemplateFromFile(filename).evaluate().getContent()
}
/**
* standard function for deploying a apps script project as an web app
* @param {event object} e
*/
function doGet(e){
let template = HtmlService.createTemplateFromFile("index.html")
let htmlOuput = template.evaluate()
htmlOuput
.addMetaTag("viewport", "width=device-width,initial-scale=1,minimal-ui")
.setTitle("Pendaftaran Solat Aidiladha & Solat Jumaat 31 Jul 2020")
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
return htmlOuput
}
/**
* find sheet by name from current spreadsheet and create a new one if it's not found
* @param {string} sheetname
*/
function getSheetByName(sheetname){
let ss = SpreadsheetApp.getActive()
let ws = ss.getSheetByName(sheetname)
if ( !ws ){
ws = ss.insertSheet(sheetname)
}
return ws
}
/**
* save response to sheet and return a registration number
* @param {stringfied JSON object} data
*/
function saveDataToSheet(data){
let uid = createUid()
if (Number(uid) > _max_responses){
return `Sorry, your response reached the limit of ${_max_responses}.`
}else{
let {sheetname, values, headers} = JSON.parse(data)
values = [new Date()].concat(values)
headers = ["Timestamp"].concat(headers)
let ws = getSheetByName(sheetname)
headers.push("Registration number")
values.push("'" + uid)
ws.getRange(1, 1, 1, headers.length).setValues([headers])
ws.appendRow(values)
return uid
}
}
function createUid(){
let props = PropertiesService.getScriptProperties()
let id = props.getProperty(uid_key)
if (id === null){
id = 1
}
id = Number(id)
let uid = (10 ** uid_length + id).toString().slice(1)
uid = uid_prefix + uid
id ++
props.setProperty(uid_key, id)
console.log(uid)
return uid
}
function resetForm(){
let props = PropertiesService.getScriptProperties()
props.deleteProperty(uid_key)
}
function openWebApp(){
let url = ScriptApp.getService().getUrl()
let html = `<script>window.open("${url}");google.script.host.close();<\/script>`
let userInterface = HtmlService.createHtmlOutput(html).setTitle("Opening the web app...")
SpreadsheetApp.getActive().show(userInterface)
}