Created
October 5, 2017 14:31
-
-
Save TonyRenHK/047a3fc5812f96a592c4f9b773e6ae32 to your computer and use it in GitHub Desktop.
Daily Note Google App Script Application
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
/** | |
Link : https://script.google.com/macros/s/AKfycbxYJhxct6evZHWRUkgXk_ijuj0mgRbnMUf9xOn3IXi8iAFoPB70/exec | |
*/ | |
function doGet() { | |
return HtmlService.createHtmlOutputFromFile('index') | |
.setSandboxMode(HtmlService.SandboxMode.IFRAME).addMetaTag('viewport', 'width=device-width, initial-scale=1'); | |
} | |
/* Loading Data From Database */ | |
function LoadingData(varr) { | |
var folderName = 'TonyApp'; | |
var folder, FileId; | |
var isOld = true; | |
var ReturnList = []; | |
var folders = DriveApp.getFoldersByName(folderName); | |
if (folders.hasNext() || folders != null) { | |
while (folders.hasNext()) { | |
folder = folders.next(); | |
} | |
} else { | |
isOld = false; | |
} | |
if (isOld) { | |
var filesExcel = DriveApp.searchFiles('title ="DRecordDB"'); | |
if (filesExcel.hasNext() || filesExcel != null) { | |
while (filesExcel.hasNext()) { | |
FileId = filesExcel.next().getId(); | |
} | |
} else { | |
isOld = false; | |
} | |
} | |
if (isOld) { | |
var Datas = SpreadsheetApp.openById(FileId); | |
var data = Datas.getActiveSheet().getDataRange().getValues(); | |
for (i in data) { // Logger.log(data[i][1]); | |
var tempObj = { | |
Id: data[i][0], | |
Title: data[i][1], | |
Description: data[i][2], | |
CreatedTime: String(data[i][3]) | |
}; | |
ReturnList.push(tempObj); | |
} | |
} | |
Logger.log(ReturnList); | |
return JSON.stringify(ReturnList); | |
} | |
function ConnectSaveRecord(inputObject) { | |
SaveRecord(inputObject); | |
return 'Save Success!'; | |
} | |
/* SaveRecord to google spreadsheet */ | |
function SaveRecord(inputObject) { | |
var folderName = 'TonyApp'; | |
var folder; | |
var isNew = false; | |
var folders = DriveApp.getFoldersByName(folderName); // replace by the right folder name, assuming there is only one folder with this name | |
if (!folders.hasNext() || folders == null) { | |
Logger.log('Failed'); | |
folder = DriveApp.createFolder(folderName); | |
} else { | |
while (folders.hasNext()) { | |
folder = folders.next(); | |
} | |
} //inputObject = {Link:"digdigme.com", Title:"DigMe - Test"}; | |
var filesExcel = DriveApp.searchFiles('title ="DRecordDB"'); | |
var FileId; | |
if (!filesExcel.hasNext() || filesExcel == null) { | |
Logger.log('No exist File, Need to create new Database'); | |
var ssNew = SpreadsheetApp.create("DRecordDB"); | |
ssNew.appendRow(["Id", "Title", "Description", "CreatedTime"]); | |
FileId = ssNew.getId(); | |
isNew = true; | |
} else { // | |
Logger.log('exist File'); | |
while (filesExcel.hasNext()) { | |
FileId = filesExcel.next().getId(); | |
} | |
} //End IF-Else: Get File ID | |
var Datas = SpreadsheetApp.openById(FileId); // Logger.log(Datas.getName()); | |
var CurrentTime = new Date(); | |
var UniqueId = new Date().getTime(); | |
Datas.appendRow([UniqueId, inputObject.Title, inputObject.Description, CurrentTime]); | |
if (isNew) { | |
var copyFile = DriveApp.getFileById(FileId); | |
folder.addFile(copyFile); | |
DriveApp.getRootFolder().removeFile(copyFile); | |
} | |
} |
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 lang="en"> | |
<head> | |
<base target="_top"> | |
<title>Personal Management</title> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css"> | |
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.2/css/buttons.dataTables.min.css"> | |
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.2.2/css/buttons.bootstrap.min.css"> | |
<link rel="stylesheet" href="https://cdn.rawgit.com/LikeHongKong/Font-Awesome/master/css/font-awesome.min.css"> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> | |
<script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script> | |
<script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script> | |
<script src="https://cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script> | |
<script src="https://cdn.datatables.net/buttons/1.2.2/js/buttons.bootstrap.min.js"></script> | |
<script> | |
$(document).ready(function() { | |
RetrieveData(); | |
}); | |
function RetrieveData() { | |
var tempHTML = '<i class="fa fa-spinner fa-pulse fa-3x fa-fw"></i> Loading...'; | |
$('#ResultTable').html(tempHTML); | |
var temp2 = google.script.run.withSuccessHandler(GetDBData).withFailureHandler(FailureHandler).LoadingData('123'); | |
} | |
function FailureHandler(error) { | |
//console.log('-----error--------');console.log(error); | |
$('#ResultTable').html(''); | |
} | |
function GetDBData(ResultData) { // | |
//console.log('Enter1');//console.log(ResultData); | |
var options = { | |
weekday: "long", | |
year: "numeric", | |
month: "short", | |
day: "numeric", | |
hour: "2-digit", | |
minute: "2-digit" | |
}; | |
var ContentData = JSON.parse(ResultData); | |
var tablehtml = '<table id="ResultData" class="table table-striped table-bordered" cellspacing="0" width="100%" > ' + | |
'<thead><tr><th style="display:none" >No</th><th style="width:12%" >Time</th><th style="width:80%" >Content</th> </tr></thead>'; | |
tablehtml += '<tbody>'; | |
//Get Total Number of Records | |
var CountNum = ContentData.length; | |
do { | |
var tempobj = ContentData.pop(); // Time console.log(tempobj.c[0].f); //Content | |
if (tempobj.Description != null) { | |
if (CountNum !== 1) { | |
CountNum--; | |
var CreatedTime = tempobj.CreatedTime.split('GMT+0800')[0] | |
tablehtml += '<tr><td style="display:none" >' + CountNum + '</td><td style="font-size: 11px;" >' + CreatedTime + '</td><td>' + tempobj.Description + '</td></tr>'; | |
} | |
} | |
} while (ContentData.length > 0); | |
tablehtml += '</tbody></table>'; | |
$('#ResultTable').html(tablehtml); | |
$('#ResultData').DataTable({ | |
"order": [ | |
[0, "desc"] | |
], | |
"dom": '<"top">rt<"bottom"Bfpil><"clear">', | |
"iDisplayLength": 50, | |
buttons: [{ | |
text: 'Write New One', | |
action: function(e, dt, node, config) { | |
window.location.href = "#"; // console.log( 'Button activated' ); | |
} | |
}] | |
}); | |
$('#ResultData').on('page.dt', function() {//console.log('change Page'); | |
$('html, body').animate({ | |
scrollTop: 0 | |
}, 700); | |
}).dataTable(); | |
} | |
function ClearFunction() { | |
$("#Title").val(''); | |
$("#Description").val(''); | |
$('#InfoMessage').html(''); | |
} | |
/* | |
Save Record function | |
*/ | |
function SaveFunction() { | |
var inputobject = { | |
Title: $("#Title").val(), | |
Description: $("#Description").val(), | |
}; | |
console.log(inputobject); | |
var temp = google.script.run.withSuccessHandler(GetSaveResult).ConnectSaveRecord(inputobject); | |
} | |
/* | |
Success Case Handler | |
*/ | |
function GetSaveResult(ResultData) { | |
//console.log(ResultData);//console.log('Save Success!'); | |
//InfoMessage | |
var innerHTML = '<div class="alert alert-success fade in"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <strong>Save Success!</strong> </div>'; | |
$('#InfoMessage').html(innerHTML); | |
RetrieveData(); | |
} | |
function ToggleInputForm() { | |
clearalert(); | |
$("#Paneldiv").toggle(); | |
} | |
function clearalert() { | |
$('#InfoMessage').html(''); | |
} | |
</script> | |
<style> | |
.rowcss { | |
padding-top: 1em; | |
padding-left: 1em; | |
padding-right: 1em; | |
} | |
</style> | |
</head> | |
<body> | |
<nav class="navbar navbar-inverse"> | |
<div class="container-fluid"> | |
<div class="navbar-header"> | |
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
<span class="icon-bar"></span> | |
</button> | |
<a class="navbar-brand" href="#">Personal Management</a> | |
</div> | |
<div class="collapse navbar-collapse" id="myNavbar"> | |
<ul class="nav navbar-nav"> | |
<li class="active"><a href="#">Home</a></li> | |
<li class="dropdown"> | |
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Me<span class="caret"></span></a> | |
<ul class="dropdown-menu"> | |
<li><a onclick="changeNoteManagement();">Note Management </a></li> | |
<li><a href="#">Password Management</a></li> | |
<li><a href="#">To do List</a></li> | |
</ul> | |
</li> | |
<li><a href="#">Diary Management </a></li> | |
<li><a href="#">BookMark Management</a></li> | |
</ul> | |
<ul class="nav navbar-nav navbar-right"> | |
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li> | |
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li> | |
</ul> | |
</div> | |
</div> | |
</nav> | |
<div class="container"> | |
<div class="row" style="padding-left:1em; padding-right:1em;"> | |
<div class="panel panel-primary"> | |
<div class="panel-heading"> <a onclick="ToggleInputForm();" style="font-weight: bold;color:white">D Record</a></div> | |
<div class="panel-body" id="Paneldiv"> | |
<div class="row" id="InfoMessage" style="padding-left:1em; padding-right:1em;"> | |
</div> | |
<div class="row" style="padding-left:1em; padding-right:1em;"> | |
<label for="Title">Title:</label> | |
<input type="text" class="form-control" id="Title" placeholder="Input Title"> | |
</div> | |
<div class="row rowcss form-group"> | |
<label for="comment">Description:</label> | |
<textarea rows="16" id="Description" class="form-control"></textarea> | |
</div> | |
<div class="row rowcss"> | |
<button type="button" class="btn btn-primary" onclick="ClearFunction();">Clear</button> | |
<button type="button" class="btn btn-primary" onclick="SaveFunction();">Save</button> | |
</div> | |
</div> | |
</div> | |
</div> | |
<div class="row" style="padding-left:1em; padding-right:1em;" id="ResultTable"> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment