-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.gs
275 lines (229 loc) · 12.6 KB
/
code.gs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var ss = SpreadsheetApp.openById('1Sh4X2OHtiQMyFYKY-SaKTny10IJK9kKY7zw_lAebd68');
var sheet = ss.getSheetByName("Regular");
var timezone = Session.getScriptTimeZone();
var str = "";
var dict = {"AdminSP" : "[email protected]", "AdminFA" : "[email protected]"}
function onFormSubmit(){
var required_sheet = ss.getSheetByName("Responses");
Utilities.sleep(500);
var nextRow = required_sheet.getLastRow();
var id = required_sheet.getRange("B"+nextRow).getValue().toString();
var extension = required_sheet.getRange("C"+nextRow).getValue().toString();
Utilities.sleep(500);
var idx = onSearch(id);
if(idx!=-1) sheet.getRange("K"+idx).setValue(extension);
}
// For Archiving
// function blurAndLockCells() {
// var sheet = SpreadsheetApp.getActiveSheet();
// var range = sheet.getRange("A1:B10"); // change range to desired cells
// // blur cells
// range.setBackgroundRGB(220, 220, 220);
// // lock cells
// range.setLocked(true);
// // protect sheet
// var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET);
// if (protections.length == 0) {
// sheet.protect().setDescription('Blurred and locked cells');
// } else {
// protections[0].setLocked(true);
// }
// }
// function transferData() {
// var ss1 = SpreadsheetApp.getActiveSpreadsheet();
// var sheet1 = ss1.getSheetByName("Sheet1");
// var data = sheet1.getRange("A1").getValue();
// var ss2 = SpreadsheetApp.openById("ID_OF_SHEET2");
// var sheet2 = ss2.getSheetByName("Sheet2");
// sheet2.getRange("B2").setValue(data);
// }
function doGet(e){
doPost(e);
return ContentService.createTextOutput(str);
}
function doPost(e){
var parsedData;
try {
parsedData = JSON.parse(e.postData.contents);
}
catch(f){
return ContentService.createTextOutput("Error in parsing request body: " + f.message);
}
if (parsedData !== undefined){
var flag = parsedData.format;
if (flag === undefined){
flag = 0;
}
var dataArr = parsedData.values.split(","); // creates an array of the values to publish
var value1 = dataArr [0]; // value0 from Arduino code, id
var value2 = dataArr [1]; // value1 from Arduino code, name
}
//----------------------------------------------------------------------------------
// var value1 = e.parameters.id; // value0 from Arduino code, id
// var value2 = e.parameters.name; // value1 from Arduino code, name
var Deploy_Date = new Date();
var Deploy_Time = Utilities.formatDate(Deploy_Date, timezone, 'HH:mm:ss');
var ID = stripQuotes(value1); // e.parameters.id
var Reader_name = stripQuotes(value2); // e.parameters.name
var index = onSearch(ID);
//----------------------------------------------------------------------------------
if(index==-1){
var nextRow = sheet.getLastRow() + 2;
sheet.getRange("A" + nextRow).setValue(ID);
sheet.getRange("B" + nextRow).setValue(Reader_name);
sheet.getRange("C" + nextRow).setValue(Deploy_Date);
sheet.getRange("D" + nextRow).setValue(Deploy_Time);
sheet.getRange("E" + nextRow).setValue(0);
//----------------------------------------------------------------------------------
// Deployed Mail by reader //
var subject_Reader = "File : "+ value1+" scanned";
var range = "A"+index;
var cell_link = 'https://docs.google.com/spreadsheets/d/1Sh4X2OHtiQMyFYKY-SaKTny10IJK9kKY7zw_lAebd68/view#gid=0&range='+range;
var mail_body_Reader = "Dear "+ value2 +" ,\n\nYour file has been scanned successfully. Click here ("+cell_link+ ") to see the updated details.\n\nThanks for using smart office.\nRegards,\nIIITD smart office team";
MailApp.sendEmail(dict[value2],subject_Reader,mail_body_Reader);
SpreadsheetApp.flush();
str = "Reader name is stored in column A B C D";
return ContentService.createTextOutput("Reader name is stored in column A B C D");
//----------------------------------------------------------------------------------
}
else if(index!=-1){
var value = sheet.getRange("F"+index);
if(value.isBlank()){
var prevData = sheet.getRange("B"+index).getValue();
if(prevData == value2.toString()){
str = "Receiving Data has already been stored";
return ContentService.createTextOutput("Receiving Data has already been stored");
}
else{
var entry = +sheet.getRange("E" + index).getValue();
var lastIndex = +entry +1;
sheet.getRange("E" + index).setFormula(entry+1);
makeChanges(e,index,value2);
var range = "A"+index+":"+"J"+lastIndex;
var cell_link = 'https://docs.google.com/spreadsheets/d/1Sh4X2OHtiQMyFYKY-SaKTny10IJK9kKY7zw_lAebd68/view#gid=0&range='+range;
var subject_Reader = "File : "+ value1+" scanned";
var mail_body_Reader = "Dear "+ value2 +" ,\n\nYour file has been scanned successfully. Click here (" +cell_link+ ") to see the updated details.\n\nIf you wish to extend the deadline, please fill this form.\n\nThanks for using smart office.\nRegards,\nIIITD smart office team";
var html = '<iframe src="https://docs.google.com/forms/d/e/1FAIpQLSdHiar1n7VdOmgdSHzNoLWE4jDwkSsK7NcRBq3dG3LxuN_sTg/viewform?embedded=true" width="640" height="768" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>';
MailApp.sendEmail(dict[value2],subject_Reader,mail_body_Reader,{htmlBody:html});
var subject_sender = "File : "+ value1 +" received by "+ value2 +" ";
var mail_body_Sender = "Dear "+ sheet.getRange("B"+index).getValue().toString() +" ,\n\nYour file( " + value1 + " ) has been received by "+ value2 +" . Click here (" +cell_link+ ") to see the updated details.\n\nThanks for using smart office.\nRegards,\nIIITD smart office team";
MailApp.sendEmail(dict[sheet.getRange("B"+index).getValue()],subject_sender,mail_body_Sender);
}
}
else{
var Deptdata = value.getValue();
if(Deptdata == value2.toString()){
str = "Receiving Data has already been stored";
return ContentService.createTextOutput("Receiving Data has already been stored");
}
else{
var entry = +sheet.getRange("E" + index).getValue();
var lastIndex = +entry+1 ;
sheet.getRange("E" + index).setFormula(entry+1);
var tempIndex = index+1;
sheet.insertRowAfter(index);
sheet.getRange("F"+index+":"+"K"+index).copyValuesToRange(sheet.getRange("F"+index+":"+"K"+index).getGridId(),6,12,tempIndex,tempIndex);
sheet.getRange("F"+index+":"+"K"+index).clearContent();
makeChanges(e,index,value2);
var range = "A"+index+":"+"J"+lastIndex;
var cell_link = 'https://docs.google.com/spreadsheets/d/1Sh4X2OHtiQMyFYKY-SaKTny10IJK9kKY7zw_lAebd68/view#gid=0&range='+range;
var subject_Reader = "File : "+ value1+" scanned";
var mail_body_Reader = "Dear "+ value2 +" ,\n\nYour file has been read scanned successfully. Click here (" +cell_link+ ") to see the updated details.\n\nIf you wish to extend the deadline, please fill this form.\n\nThanks for using smart office.\nRegards,\nIIITD smart office team";
var html = '<iframe src="https://docs.google.com/forms/d/e/1FAIpQLSdHiar1n7VdOmgdSHzNoLWE4jDwkSsK7NcRBq3dG3LxuN_sTg/viewform?embedded=true" width="640" height="768" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>';
MailApp.sendEmail(dict[value2],subject_Reader,mail_body_Reader,{htmlBody:html});
var subject_sender = "File : "+ value1 +" received by "+ value2 +" ";
var next_index = +index+1;
var mail_body_Sender = "Dear "+ sheet.getRange("F"+next_index).getValue().toString() +" ,\n\nYour file( " + value1 + " ) has been received by "+ value2 +" . Click here (" +cell_link+ ") to see the updated details.\n\nThanks for using smart office.\nRegards,\nIIITD smart office team";
MailApp.sendEmail(dict[sheet.getRange("F"+next_index).getValue().toString()],subject_sender,mail_body_Sender);
}
}
}
}
function makeChanges(e,index,value2){
//----------------------------------------------------------------------------------
var Receiving_Date = new Date();
var Receiving_Time = Utilities.formatDate(Receiving_Date, timezone, 'HH:mm:ss');
var Receiving_Dept = value2; // e.parameters.name
//----------------------------------------------------------------------------------
sheet.getRange("F" + index).setValue(Receiving_Dept);
sheet.getRange("G" + index).setValue(Receiving_Date);
sheet.getRange("H" + index).setValue(Receiving_Time);
var tempIndex = +index+1;
if(sheet.getRange("H" + tempIndex).isBlank()){
sheet.getRange("I" + index).setFormula('=ArrayFormula(IFERROR(if(Len(A'+index+'),(int(G'+index+'-C'+index+')&"d "&text(G'+index+'-C'+index+'-int(G'+index+'-C'+index+'),"HH:MM:SS")),)))');
}
else{
sheet.getRange("I" + index).setFormula('=ArrayFormula(IFERROR(if(Len(A'+index+'),(int(G'+index+'-G'+tempIndex+')&"d "&text(G'+index+'-G'+tempIndex+'-int(G'+index+'-G'+tempIndex+'),"HH:MM:SS")),)))');
}
var Dept1 = sheet.getRange("B"+index).getValue();
var Dept2 = sheet.getRange("F"+index).getValue();
var lastIndex = +sheet.getRange("E"+index).getValue();
if(Dept1 == Dept2){
// var lastIndex = +sheet.getRange("E"+index).getValue();
sheet.getRange("A"+index).setValue(sheet.getRange("A"+index).getValue()+"/F");
sheet.getRange("J"+index).setValue("Finished");
sheet.getRange(index,1,lastIndex,10).setBackground("#44cfbf");
}
//----------------------------------------------------------------------------------
str = "Receiving Data is stored in column F G H I J";
SpreadsheetApp.flush();
return ContentService.createTextOutput("Receiving Data is stored in column F G H I J");
//----------------------------------------------------------------------------------
}
function delay(){
for(var i = 3; i<sheet.getLastRow(); i++){
if(!sheet.getRange("A"+i).isBlank()){
if(sheet.getRange("J"+i).getValue()=="Finished"){
continue;
}
else{
var baseDate = (new Date() - sheet.getRange("G"+i).getValue())/1000/60/60/24;
baseDate = parseInt(baseDate);
// Logger.log(baseDate);
if(baseDate>=2){
// Remainder Mail //
var index = +i;
var lastIndex = +sheet.getRange("E"+index).getValue();
var file_id = sheet.getRange("A"+index).getValue().toString();
var range = "A"+index+":"+"J"+lastIndex;
var cell_link = 'https://docs.google.com/spreadsheets/d/1Sh4X2OHtiQMyFYKY-SaKTny10IJK9kKY7zw_lAebd68/view#gid=0&range='+range;
var sender = "";
var receiver = "";
var j = +i+1;
if(sheet.getRange("F"+j).isBlank()){
sender = sheet.getRange("A"+i).getValue().toString();
receiver = sheet.getRange("F"+i).getValue().toString();
}
else{
sender = sheet.getRange("F"+j).getValue().toString();
receiver = sheet.getRange("F"+i).getValue().toString();
}
var subject = "Reminder:- Please process the pending file (" + file_id +" )";
var body = "The file(" + file_id +") was submitted by "+ sender +" on the date-( "+sheet.getRange("G"+i).getValue()+" ) and has been in your department for the last * days( "+ baseDate+" ). Click here ("+cell_link +") to see further details. If you wish to extend the deadline, please fill this form.\n\nThanks for using smart office.\nRegards,\nIIITD smart office team";
var html = '<iframe src="https://docs.google.com/forms/d/e/1FAIpQLSdHiar1n7VdOmgdSHzNoLWE4jDwkSsK7NcRBq3dG3LxuN_sTg/viewform?embedded=true" width="640" height="768" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>';
MailApp.sendEmail(dict[receiver],subject,body,{htmlBody:html});
}
}
}
}
}
function stripQuotes( value ) {
return value.toString().replace(/^["']|['"]$/g, "");
}
function onSearch(id){
var searchString = id.toString();
var Sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Regular");
var column = 1; //column Index
var columnValues = Sheet.getRange(2, column, Sheet.getLastRow()).getValues(); //1st is header row
var searchResult = columnValues.findIndex(searchString); //Row Index - 2
if(searchResult != -1){
return searchResult + 2;
}
return searchResult;
}
Array.prototype.findIndex = function(search){
if(search == "") return false;
for (var i=0; i<this.length; i++)
if (this[i] == search) return i;
return -1;
}