Last active
December 24, 2019 13:24
-
-
Save oshliaer/3fa2ced154c5bec393d6 to your computer and use it in GitHub Desktop.
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 FOLDER_ID = '0Bztea6vSatozflJSckhvNk55VXNXVExHTlBQSl9Qd21nM0dDdTRnbWJPdjhLZmhKLXZidHM'; | |
var SHEET_ID = '1toHBN8rPp9d0GFQQY8XEypMS-CkKoMAvcfQMkyulrR0'; | |
//вместо инициализации | |
function onInstall() { } | |
//сервеер | |
function doGet(e) { | |
//возвращает страницу | |
return HtmlService.createHtmlOutputFromFile('index').addMetaTag('viewport', 'width=device-width, initial-scale=1').setSandboxMode(HtmlService.SandboxMode.IFRAME); | |
} | |
//обработчик вызова сервера | |
function uploadFiles(form) { | |
//все в try, иначе придется обрабатывать исключения на клиенте | |
try { | |
//берем нашу папку | |
var folder = DriveApp.getFolderById(FOLDER_ID); | |
var res = []; | |
//сортировка полей по имени | |
var form_keys = Object.keys(form).sort(); | |
/* | |
или задайте свою сортировку | |
var form_keys = ['file1', 'file2']; | |
*/ | |
for (var i = 0; i < form_keys.length; i++) { | |
var key = form_keys[i]; | |
if (form[key] && form[key].getBlob) { | |
if (form[key].length && form[key].contents) { | |
//берем контент file | |
var blob = form[key]; | |
//создаем файл на Диске | |
var file = folder.createFile(blob); | |
file.setDescription('Uploaded by ' + form['name'] + ' ' + form['comment']); | |
res.push(file.getUrl()); | |
} else { | |
// поле file оказалось пустым | |
res.push('Пусто'); | |
} | |
} | |
} | |
//делаем запись в Таблице в первом найденном листе (Необходимо переписать для большей логики) | |
SpreadsheetApp.openById(SHEET_ID).getSheets()[0].appendRow([new Date(), form['name'], form['comment']].concat(res)); | |
//ответ сервера | |
/* | |
return res; | |
return 'Файл(ы) загружен(ы), йо! ' + res.join(', '); | |
*/ | |
return 'Файл(ы) загружен(ы), йо!'; | |
} catch (e) { | |
//обработаем любую ошибку как ответ сервера "ОК" 200 | |
return e.toString(); | |
} | |
} |
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 gulp = require('gulp'); | |
var fs = require('fs'); | |
var del = require('del'); | |
var exec = require('child_process').exec; | |
var replace = require('gulp-replace'); | |
var debug = require('gulp-debug'); | |
gulp.task('gs', ['googleanalytics'], function (cb) { | |
return gulp.src(['./*.gs']).pipe(gulp.dest('src')); | |
}); | |
gulp.task('clean', function () { | |
return del.sync(['src/*']); | |
}); | |
gulp.task('gapps', ['gs'], function (cb) { | |
console.log('googleanalytics'); | |
exec('gapps push', function (err, stdout, stderr) { | |
console.log(stdout); | |
console.log(stderr); | |
cb(err); | |
}); | |
}) | |
gulp.task('googleanalytics', ['clean'], function () { | |
gulp.src(['./index.html'], { base: './' }) | |
.pipe(debug()) | |
.pipe(replace('<!-- googleanalytics.html -->', function (match) { | |
return fs.readFileSync('./googleanalytics.html'); | |
})) | |
.pipe(gulp.dest('src')); | |
}); | |
gulp.task('default', ['gapps'], function () { | |
return; | |
}) |
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> | |
<base target="_top"> | |
<!-- CSS от Google Add-on --> | |
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> | |
<!-- Немного своего стиля --> | |
<style> | |
#app { | |
margin: 55px auto; | |
width: 350px; | |
} | |
/* Перепишем кое-что из add-ons.css */ | |
input { | |
width: 100%; | |
} | |
.form-group { | |
margin: 9px 0px 9px 0px; | |
} | |
</style> | |
<!-- googleanalytics.html --> | |
</head> | |
<body> | |
<!-- основное приложение --> | |
<div id="app"> | |
<!-- форма --> | |
<form id="form"> | |
<div class="form-group"> | |
<label for="name">Имя</label> | |
<input type="text" name="name" placeholder="Имя" /> | |
</div> | |
<div class="form-group"> | |
<label for="comment">Комм</label> | |
<input type="text" name="comment" placeholder="Комментарий" /> | |
</div> | |
<div class="form-group"> | |
<input type="file" name="file1" /> | |
</div> | |
<div class="form-group"> | |
<input type="file" name="file2" /> | |
</div> | |
<label><sub><i>Суммарный вес файлов не должен превышать 10Мб</i></sub></label> | |
<div class="form-group"> | |
<input type="submit" value="Upload file" /> | |
</div> | |
</form> | |
<div id="output"></div> | |
</div> | |
<script> | |
//Обработка события onsubmit | |
var frm = document.getElementById('form'); | |
var otp = document.getElementById('output'); | |
if (frm.addEventListener) | |
frm.addEventListener('submit', submit, false); | |
//функция кнопки submit | |
function submit(event) { | |
event.preventDefault(); | |
frm.style.display = 'none'; | |
//меняем надпись | |
otp.innerHTML = 'Идет загрузка...'; | |
//вызываем сервер для обработки данных формы. Функция на стороне сервера uploadFiles(form) | |
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).uploadFiles(this); | |
} | |
//функция по завершению работы с сервером | |
function onSuccess(status) { | |
frm.style.display = 'block'; //прячем форму | |
frm.reset(); | |
otp.innerHTML = status; //показываем, что ответил сервер | |
} | |
function onFailure(e) { | |
frm.style.display = 'block'; //прячем форму | |
otp.innerHTML = JSON.stringify(e); | |
} | |
</script> | |
<!-- inject:sh --> | |
<!-- endinject --> | |
</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
{ | |
"name": "mygoogleform", | |
"version": "1.0.0", | |
"description": "", | |
"main": "code.gs", | |
"scripts": { | |
"start": "gulp" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+https://gist.github.com/3fa2ced154c5bec393d6.git" | |
}, | |
"author": "", | |
"license": "WTFPL2", | |
"bugs": { | |
"url": "https://gist.github.com/3fa2ced154c5bec393d6" | |
}, | |
"homepage": "https://gist.github.com/3fa2ced154c5bec393d6", | |
"devDependencies": { | |
"del": "^2.2.2", | |
"gulp": "^3.9.1", | |
"gulp-debug": "^3.1.0", | |
"gulp-replace": "^0.5.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Добавить до ответа сервера
https://plus.google.com/105480244697531087901/posts/Qa2644UVM5w