Created
July 11, 2016 06:59
-
-
Save kir-sf/77e00c1b5ff51563a975b100231d4335 to your computer and use it in GitHub Desktop.
Signer
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
function doGet(e) { | |
return HtmlService.createTemplateFromFile('index').evaluate() | |
.setSandboxMode(HtmlService.SandboxMode.IFRAME); | |
} | |
function include(filename) { | |
return HtmlService.createHtmlOutputFromFile(filename) | |
.getContent(); | |
} | |
// загрузка файла на Диск | |
function uploadFile(form) { | |
try { | |
var folder=DriveApp.getFolderById(getUserFolderId(Session.getActiveUser().getEmail())); | |
var file=folder.createFile(form.file); | |
var date=new Date(); | |
var month=date.getMonth()+1; | |
if (month<10) month='0'+month; | |
file.setDescription("Загружен "+Session.getActiveUser().getEmail()+" "+date.getDate()+"/"+month+"/"+date.getFullYear()); | |
prepareFileToSign(file); | |
return true; | |
} catch(err) { | |
return(JSON.stringify) | |
} | |
} | |
// формирование HTML списка файлов на подпись | |
function getHtmlListToSign() { | |
var files=getFilesToSign(); | |
var listToSign=''; | |
for (var i=0; i<files.length; i++) { | |
listToSign=listToSign+'<div class="form-element">'; | |
if (isUserSigner(Session.getActiveUser().getEmail())) | |
listToSign=listToSign+'<button class="share" value="'+files[i].id+'" onclick="signFile(this.value)"> Подписать </button>'; | |
listToSign=listToSign+'<button class="create" value="'+files[i].id+'" onclick="deleteFileFromToSign(this.value)"> Удалить </button>'; | |
listToSign=listToSign+'<a href="'+files[i].link+'">'+files[i].filename+'</a>' | |
listToSign=listToSign+files[i].description+'</div>'; | |
} | |
return listToSign; | |
} | |
// формирование HTML списка настроек | |
function getHtmlOption() { | |
var opt=getOption(); | |
var res="<textarea rows='10' id='optionText'>"; | |
if (!opt) | |
res=res+"У Вас недостаточно прав для настройки этого приложения!"; | |
else | |
res=res+opt; | |
res=res+"</textarea>"; | |
if (!opt) | |
res=res+"<br>"; | |
else | |
res=res+"<input id='saveOption' type='button' onclick='updateOption()' value='Сохранить'/>"; | |
return res; | |
} | |
// формирование HTML списка подписанных файлов | |
function getHtmlSignedList() { | |
// готовим строку даты для заполнения значения "по умолчанию" | |
var defaultDate=new Date(); | |
if (defaultDate.getMonth()<9) | |
defaultDate=defaultDate.getFullYear()+'-0'+(defaultDate.getMonth()+1)+'-'+defaultDate.getDate(); | |
else | |
defaultDate=defaultDate.getFullYear()+'-'+(defaultDate.getMonth()+1)+'-'+defaultDate.getDate(); | |
// готовим список | |
var files=getSignedFiles(); | |
var listSigned='<table width=100%><tr class="table-header"><td>Ссылка на файл</td><td>Подпись</td><td>Номер заказа</td><td>Дата заказа</td><td>Операции с файлом</td></tr>'; | |
for (var i=0; i<files.length; i++) { | |
listSigned=listSigned+'<tr><td><a href="'+files[i].link+'">'+files[i].filename+'</a></td>' | |
listSigned=listSigned+'<td><b> '+files[i].subscriber.value+'</b></td>'; | |
listSigned=listSigned+'<td><input type="text" size="8" id="'+files[i].id+'num" />'; | |
listSigned=listSigned+'<td><input type="date" size="8" id="'+files[i].id+'date" value="'+defaultDate+'"/>'; | |
listSigned=listSigned+'<td><button class="share" value="'+files[i].id+'" onclick="saveOrder(this.value)"> Сохранить </button>'; | |
if (Session.getActiveUser().getEmail()==files[i].subscriber.value) | |
listSigned=listSigned+'<button class="create" value="'+files[i].id+'" onclick="deleteFileFromSigned(this.value)"> Удалить </button>'; | |
listSigned=listSigned+'</td></tr>'; | |
} | |
listSigned=listSigned+'</table>'; | |
return listSigned; | |
} | |
// Подпись файла | |
function signFile(fileId) { | |
try { | |
var file=DriveApp.getFileById(fileId); | |
var res="Файл "+file.getName()+" подписан"; | |
setFileSigned(file); | |
return res | |
} catch(err) { | |
return JSON.stringify(err); | |
} | |
} | |
// Удаление файла | |
function deleteFile(fileId) { | |
try { | |
var file=DriveApp.getFileById(fileId); | |
var res="Файл "+file.getName()+" удален"; | |
removeFromSigner(file); | |
return res | |
} catch(err) { | |
return JSON.stringify(err); | |
} | |
} | |
// сохранение реквизитов заказа | |
function saveOrder(fileId, orderNum, orderDate) { | |
try { | |
var file=DriveApp.getFileById(fileId); | |
setFileOrdered(file, orderNum, orderDate); | |
return true; | |
} catch(err) { | |
return JSON.stringify(err); | |
} | |
} | |
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
Array.prototype.findByPropName = function(name){ | |
for(var i = 0; i < this.length; i++){ | |
if(this[i].key == name) return this[i] | |
} | |
return {value: undefined}; | |
} | |
// Подписание файла | |
function setFileSigned(file) { | |
var currfileID=file.getId(); | |
// подпись | |
var property = { | |
key: 'subscribed', | |
value: 'true', | |
visibility: 'PUBLIC' | |
} | |
Drive.Properties.insert(property, currfileID); | |
// автор подписи | |
var property = { | |
key: 'subscriber', | |
value: Session.getActiveUser().getEmail(), | |
visibility: 'PRIVATE' | |
} | |
Drive.Properties.insert(property, currfileID); | |
} | |
// Установка файла в статус "на подпись" | |
function prepareFileToSign(file) { | |
var currfileID=file.getId(); | |
// подпись | |
var property = { | |
key: 'subscribed', | |
value: 'false', | |
visibility: 'PUBLIC' | |
} | |
Drive.Properties.insert(property, currfileID); | |
// автор подписи | |
var property = { | |
key: 'subscriber', | |
value: '', | |
visibility: 'PRIVATE' | |
} | |
Drive.Properties.insert(property, currfileID); | |
// номер заказа | |
var property = { | |
key: 'orderNum', | |
value: '', | |
visibility: 'PUBLIC' | |
} | |
Drive.Properties.insert(property, currfileID); | |
// дата заказа | |
var property = { | |
key: 'orderDate', | |
value: '', | |
visibility: 'PUBLIC' | |
} | |
Drive.Properties.insert(property, currfileID); | |
} | |
// удаление файла | |
function removeFromSigner(file) { | |
var currfileID=file.getId(); | |
Drive.Properties.remove(currfileID, 'subscribed', {visibility: 'PUBLIC'}); | |
Drive.Properties.remove(currfileID, 'subscriber', {visibility: 'PRIVATE'}); | |
Drive.Properties.remove(currfileID, 'orderNum', {visibility: 'PUBLIC'}); | |
Drive.Properties.remove(currfileID, 'orderDate', {visibility: 'PUBLIC'}); | |
var folders=file.getParents(); | |
while (folders.hasNext()) { | |
var folder=folders.next(); | |
folder.removeFile(file); | |
} | |
file.setTrashed(true); | |
} | |
// Список файлов на подпись | |
function getFilesToSign() { | |
var query="properties has {key='subscribed' and value='false' and visibility='PUBLIC'}"; | |
var files=Drive.Files.list({q: query}); | |
var res=[]; | |
for (var index=0; index<files.items.length; index++) { | |
var file=files.items[index]; | |
var element={ | |
id: file.id, | |
filename: file.title, | |
link: file.alternateLink, | |
description: file.description, | |
subscribed: file.properties.findByPropName('subscribed'), | |
subscriber: file.properties.findByPropName('subscriber'), | |
orderNum: file.properties.findByPropName('orderNum'), | |
orderDate: file.properties.findByPropName('orderDate') | |
} | |
res.push(element); | |
} | |
return res; | |
} | |
// Список подписанных файлов | |
function getSignedFiles() { | |
var query="properties has {key='subscribed' and value='true' and visibility='PUBLIC'} and properties has {key='orderNum' and value='' and visibility='PUBLIC'}"; | |
var files=Drive.Files.list({q: query}); | |
var res=[]; | |
for (var index=0; index<files.items.length; index++) { | |
var file=files.items[index]; | |
var element={ | |
id: file.id, | |
filename: file.title, | |
link: file.alternateLink, | |
subscribed: file.properties.findByPropName('subscribed'), | |
subscriber: file.properties.findByPropName('subscriber'), | |
orderNum: file.properties.findByPropName('orderNum'), | |
orderDate: file.properties.findByPropName('orderDate') | |
} | |
res.push(element); | |
} | |
return res; | |
} | |
// Запись реквизитов заказа в файл | |
function setFileOrdered(file, orderNum, orderDate) { | |
var currfileID=file.getId(); | |
// номер заказа | |
var property = { | |
key: 'orderNum', | |
value: orderNum, | |
visibility: 'PUBLIC' | |
} | |
Drive.Properties.insert(property, currfileID); | |
// дата заказа | |
var property = { | |
key: 'orderDate', | |
value: new Date(orderDate), | |
visibility: 'PUBLIC' | |
} | |
Drive.Properties.insert(property, currfileID); | |
} |
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
<base target="_top"> | |
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>SIGNER v2.0</title> | |
<?!= include('headers'); ?> | |
<?!= include('styles'); ?> | |
<?!= include('scripts'); ?> | |
</head> | |
<body> | |
<!-- рабочая область --> | |
<div class="my-desktop"> | |
<!-- форма загрузки файла --> | |
<div class="app-form" id="uploadFile"> | |
<div class="caption">Загрузка файла</div> | |
<form id="formAddFile"> | |
<div class="form-element"> | |
<input type="file" name="file" id="file"/> | |
<input type="button" onclick="uploadFile()" value="Добавить"/> | |
<input type="reset"/> | |
</div> | |
<div id="output">Выберите файл и нажмите <b>Добавить</b> для загрузки его на Гугл Диск</div> | |
</form> | |
</div> | |
<!-- список файлов на подпись --> | |
<div class="app-form" id="filesToSign"> | |
<div class="caption">Файлы на подпись</div> | |
<div id="listToSign"></div> <br> | |
</div> | |
<!-- список подписанных файлов --> | |
<div class="app-form" id="signedFiles"> | |
<div class="caption">Подписанные файлы</div> | |
<div id="signedList"></div> <br> | |
</div> | |
</div> | |
<script>Show()</script> | |
</body> | |
</html> |
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 OPTION_ID='1ufmjqtNTtwLphYWWv_pTgLy5P4B7QbEwEIFB10tBWno'; | |
function isUserSigner(email) { | |
var doc=DocumentApp.openById(OPTION_ID); | |
var text=doc.getBody().getText(); | |
var element=JSON.parse(text); | |
Logger.log(element); | |
for (var i=0; i<element.signer.length; i++) | |
if (element.signer[i]==email) return true; | |
return false; | |
} | |
function getUserFolderId(email) { | |
var doc=DocumentApp.openById(OPTION_ID); | |
var text=doc.getBody().getText(); | |
var element=JSON.parse(text); | |
for (var i=0; i<element.folders.length; i++) | |
if (element.folders[i][0]==email) | |
return element.folders[i][1]; | |
return undefined; | |
} |
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
<script> | |
// скрипты загрузки файла | |
function uploadFile() { | |
document.getElementById("output").innerHTML="Идет загрузка..."; | |
google.script.run.withSuccessHandler(onLoadSuccess).withFailureHandler(onLoadFailure).uploadFile(document.getElementById("formAddFile")); | |
} | |
function onLoadFailure(err) { | |
document.getElementById("output").innerHTML=err; | |
} | |
function onLoadSuccess(status) { | |
if (status) | |
document.getElementById("output").innerHTML="Выберите файл и нажмите <b>Добавить</b> для загрузки его на Гугл Диск"; | |
else | |
document.getElementById("output").innerHTML="Ошибка загрузки"; | |
google.script.run.withSuccessHandler(showListToSign).withFailureHandler(showListToSign).getHtmlListToSign(); | |
} | |
// скрипты отображения списка | |
function Show() { | |
google.script.run.withSuccessHandler(showListToSign).withFailureHandler(showListToSign).getHtmlListToSign(); // загрузка файлов на подпись | |
google.script.run.withSuccessHandler(showSignedList).withFailureHandler(showSignedList).getHtmlSignedList(); // загрузка подписанных файлов | |
google.script.run.withSuccessHandler(showOption).withFailureHandler(showOption).getHtmlOption(); // загрузка настроек | |
google.script.run.withSuccessHandler(showAbout).withFailureHandler(showAbout).getHtmlAbout(); // загрузка справки | |
} | |
function showListToSign(res) { | |
document.getElementById("listToSign").innerHTML=res; | |
} | |
function showSignedList(res) { | |
document.getElementById("signedList").innerHTML=res; | |
} | |
function showOption(res) { | |
document.getElementById("optionOutput").innerHTML=res; | |
} | |
function showAbout(res) { | |
document.getElementById("aboutOutput").innerHTML=res; | |
} | |
// скрипты подписи и удаления файлов | |
function signFile(fileId) { | |
google.script.run.withSuccessHandler(onSignProcess).withFailureHandler(onSignProcess).signFile(fileId); | |
} | |
function onSignProcess(res) { | |
alert(res); | |
google.script.run.withSuccessHandler(showListToSign).withFailureHandler(showListToSign).getHtmlListToSign(); | |
google.script.run.withSuccessHandler(showSignedList).withFailureHandler(showSignedList).getHtmlSignedList(); | |
} | |
function deleteFileFromToSign(fileId) { | |
google.script.run.withSuccessHandler(onDeleteFromToSignProcess).withFailureHandler(onDeleteFromToSignProcess).deleteFile(fileId); | |
} | |
function onDeleteFromToSignProcess(res) { | |
alert(res); | |
google.script.run.withSuccessHandler(showListToSign).withFailureHandler(showListToSign).getHtmlListToSign(); | |
} | |
function deleteFileFromSigned(fileId) { | |
google.script.run.withSuccessHandler(onDeleteFromSignedProcess).withFailureHandler(onDeleteFromSignedProcess).deleteFile(fileId); | |
} | |
function onDeleteFromSignedProcess(res) { | |
alert(res); | |
google.script.run.withSuccessHandler(showSignedList).withFailureHandler(showSignedList).getHtmlSignedList(); | |
} | |
// сохранение заказа | |
function saveOrder(val) { | |
var orderNum=document.getElementById(val+'num').value; | |
var orderDate=document.getElementById(val+'date').value; | |
if (orderNum.length!=8) {alert('Длина номера заказа должна быть 8 символов!'); return} | |
try { | |
var day=orderDate.substring(8); | |
var month=orderDate.substring(5, 7); | |
var year=orderDate.substring(0, 4); | |
if ((day.length==0)||(month.length==0)||(year.length==0)) {alert('Неверная дата!'); return} | |
var myDate=day+'/'+month+'/'+year; | |
var scriptDate=month+'/'+day+'/'+year; | |
if (confirm("Прикрепляем к файлу заказ №"+orderNum+" от "+myDate+"?")) | |
google.script.run.withSuccessHandler(onSaveProcess).withFailureHandler(onSaveProcess).saveOrder(val, orderNum, scriptDate); | |
} catch (err) { | |
alert(err); | |
} | |
} | |
function onSaveProcess(res) { | |
if (res) | |
google.script.run.withSuccessHandler(showSignedList).withFailureHandler(showSignedList).getHtmlSignedList(); | |
else | |
alert(res); | |
} | |
</script> |
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
<style> | |
.grand-title { | |
color: blue; | |
background-color: lightgray; | |
font: bold 250% fantasy; | |
} | |
.my-sidebar { | |
width: 15%; | |
height: 95%; | |
background-color: #808080; | |
padding: 0; | |
border: 4px double #e8e8e8; | |
} | |
.mymenu-button { | |
margin: 1%; | |
width: 98%; | |
} | |
.my-desktop { | |
margin-left: 5%; | |
width: 90%; | |
} | |
.form-element { | |
margin: 3px 2% 3px 2%; | |
width: 96%; | |
} | |
#file { | |
background-color: silver; | |
width: 75%; | |
} | |
.caption { | |
color: white; | |
background-color: teal; | |
font: bold 110% sans-serif; | |
} | |
.app-form { | |
margin: 10px auto; | |
width: 90%; | |
background-color: #e8e8e8; | |
} | |
#optionText { | |
width: 100%; | |
} | |
.table-header { | |
background-color: #808080; | |
color: white; | |
font: bold 100% arial; | |
padding: 3px; | |
} | |
table { | |
border-collapse: collapse; | |
border-spacing: 0; | |
} | |
th, td { | |
margin: 0; | |
padding: 1px 0; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment