ДОДАТКИ
Лістинг коду файлу code.gs
const PROJECTSHEETID = '1dzcddBeNDPj-hGR_PxSJwwoJ1XG3qs_clB1Jk1C6yJ4';
function requestAccess() {
var sheet = SpreadsheetApp.openById(PROJECTSHEETID).getSheetByName('access');
var sheetdata = sheet.getDataRange().getValues();
var user = Session.getActiveUser().getEmail();
var access = false;
for (var i = 0; i < sheetdata.length; i++) {
if (sheetdata[i][0] == user) {
access = true;
}
}
return access;
}
function doGet(e) {
var template = "user";
var title = "Трекер завдань";
var user = Session.getActiveUser().getEmail();
if ('p' in e.parameters) {
if (e.parameters['p'][0] === 'edit') {
if (requestAccess()) {
template = 'edit';
}
}
}
var t = HtmlService.createTemplateFromFile(template);
t.data = {
user: user
, tasks: listMain()
, subtask: listSub()
}
try {
var html = t.evaluate();
html.setTitle(title);
return html;
}
catch (e) {
return ContentService.createTextOutput(JSON.stringify({
"error": e
})).setMimeType(ContentService.MimeType.JSON);
}
}
function listSub() {
var sheet = SpreadsheetApp.openById(PROJECTSHEETID).getSheetByName('subtasks');
var sheetdata = sheet.getDataRange().getValues();
var data = sheetdata.slice(1);
var ep = [];
for (var i = 0; i < data.length; i++) {
var com = (!isNaN(data[i][6]) || data[i][6] == '') ? data[i][6] : 100;
var obj = {
id: data[i][1]
, mainId: data[i][0]
, row: i + 2
, created: data[i][2]
, due: data[i][3]
, assign: data[i][4]
, details: data[i][5]
, completed: com
};
ep.push(obj);
}
var output = {
status: 'status'
, data: ep
}
Logger.log(output);
return output;
}
function listMain() {
var sheet = SpreadsheetApp.openById(PROJECTSHEETID).getSheetByName('tasks');
var sheetdata = sheet.getDataRange().getValues();
var data = sheetdata.slice(1);
var ep = [];
for (var i = 0; i < data.length; i++) {
var complete = data[i][5] ? true : false;
if (!complete) {
var obj = {
id: data[i][0],
row: i + 2,
created: data[i][1],
due: data[i][2],
assign: data[i][3],
details: data[i][4],
completed: complete
};
ep.push(obj);
}
}
var output = {
status: 'status'
, data: ep
}
Logger.log(output);
return output;
}
function updateSubTask(data) {
var sheet = SpreadsheetApp.openById(PROJECTSHEETID).getSheetByName('subtasks');
var sheetdata = sheet.getRange(2, 1, sheet.getLastRow(),
sheet.getLastColumn()).getValues();
for (var i = 0; i < sheetdata.length; i++) {
if (sheetdata[i][1] == data.taskID) {
var tempArr = [[data.due, data.assign, data.details, data.complete]];
var tempRange = sheet.getRange(i + 2, 4, 1, 4);
tempRange.setValues(tempArr);
return {
status: 'success',
message: tempArr
};
}
}
return data;
}
function updateTask(data) {
var sheet = SpreadsheetApp.openById(PROJECTSHEETID).getSheetByName('tasks');
var sheetdata = sheet.getRange(2, 1, sheet.getLastRow(),
sheet.getLastColumn()).getValues();
for (var i = 0; i < sheetdata.length; i++) {
if (sheetdata[i][0] == data.taskID) {
var tempArr = [[data.due, data.assign, data.details]];
var tempRange = sheet.getRange(i + 2, 3, 1, 3);
tempRange.setValues(tempArr);
return {
status: 'success',
message: tempArr
};
}
}
return data;
}
function addSubTask(data) {
var sheet = SpreadsheetApp.openById(PROJECTSHEETID).getSheetByName('subtasks');
var userEmail = Session.getActiveUser().getEmail();
data.id = getRandom().toUpperCase();
sheet.appendRow([data.taskID, data.id, formatDate(Date()), data.due, data.assign,
data.details, data.complete, userEmail]);
data.rowValue = sheet.getLastRow();
return data;
}
function addTask(data) {
var sheet = SpreadsheetApp.openById(PROJECTSHEETID).getSheetByName('tasks');
var userEmail = Session.getActiveUser().getEmail();
data.id = getRandom().toUpperCase();
sheet.appendRow([data.id, formatDate(Date()), data.due, data.assign, data.details,
'', userEmail])
return data;
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
function getRandom() {
const uuid = Utilities.getUuid();
const shortenedId = uuid.slice(0, 8);
return shortenedId;
}
|