交互式目錄樹
這些功能與JStree jQuery插件一起工作,產生一個目錄樹互動。有幾個Id,你必須添加DataStorage文件夾的ID和根目錄在你的樹中的ID。如果你想要一個webapp,還有一個doGet功能已經在這裏完成了。有幾個文件我用於html以及一些集成到遍歷代碼中的html代碼。對我來說這是一個相當複雜的項目。有可能有更好的方法來做到這一點。
文件在數據存儲文件夾:TopToBodyWeb
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<style>.file{color:#d81840;}.contrls{padding:10px 10px 10px 10px;}.timestmp {width: 200px;margin-left:5px;padding:10px 0px 5px 50px;background-color:rgba(121, 25, 0, 0.36);color:white;}i.jstree-icon.jstree-themeicon{display:none;}</style>
<script type="text/javascript">
$(function() { $('#selector').jstree({"core" : {"themes":{"variant":"large"}},"plugins" : [ "wholerow"]});
});
</script>
</head>
<body><div id="selector"><ul>
文件在數據存儲文件夾:TopToBody
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<style>.file{color:#d81840;}.contrls{padding:10px 10px 10px 10px;}.timestmp {width: 200px;margin-left:5px;padding:10px 0px 5px 50px;background-color:rgba(121, 25, 0, 0.36);color:white;}i.jstree-icon.jstree-themeicon{display:none;}</style>
<script type="text/javascript">
$(function() { $('#selector').jstree({"core" : {"themes":{"variant":"large"}},"plugins" : [ "wholerow"]});
});
</script>
</head>
<body><div class="cntrls"><input type="button" value="Exit" onClick="google.script.host.close();" />
<input type="button" value="Delete Cache & Exit" onClick="google.script.run.clearDirlist();google.script.host.close();" /></div><div id="selector"><ul>
文件在數據存儲文件夾:BodyToEndWeb
</ul></div></body></html>
文件在數據存儲文件夾:BodyToEnd
</ul></div><div class="cntrls"><input type="button" value="Exit" onClick="google.script.host.close();" />
<input type="button" value="Delete Cache & Exit" onClick="google.script.run.clearDirlist();google.script.host.close();" /></div><br /></body></html>
數據存儲文件夾:還包含一個文件:CurrentDirectoryListing
這是Code.gs:
var GStorage = '';
var folderID = "TheIDofDataStorageFolderGoesHere";
var guli = 0;
var glii = 0;
var glevel = 0;
function onOpen()
{
var ui = SpreadsheetApp.getUi();
ui.createMenu('My Tools')
.addItem('Dir MyDrive','runtraverseFolder')
.addToUi();
}
function runFile()
{
var file = loadFile();
if(file)
{
dispStatus('Stored MyDrive Directory List', file, 1000, 600);
}
else
{
SpreadsheetApp.getUi().alter('File not found in function runfile');
}
}
function loadFile(filename)
{
var filename = (typeof(filename) !== 'undefined')? filename : 'UploadFile';
var fldr = DriveApp.getFolderById(folderID);
var file = fldr.getFilesByName(filename);
var s = '';
while(file.hasNext())
{
var fi = file.next();
var target = fi.getName();
if(target == filename)
{
s = fi.getBlob().getDataAsString();
}
}
return s;
}
function delFile(filename)
{
var filename = (typeof(filename) !== 'undefined')? filename : 'UploadFile';
var fldr = DriveApp.getFolderById(folderID)
var file = fldr.getFilesByName(filename);
var targetFound = false;
while(file.hasNext())
{
var fi = file.next();
var target = fi.getName();
if(target == filename)
{
targetFound = true;
fldr.removeFile(fi);
SpreadsheetApp.getUi().alert('File: ' + filename + ' was removed from: ' + fldr.getName() + '/' + target);
}
}
return targetFound;
}
function doGet()
{
var output=HtmlService.createHtmlOutput(getStoredDirectory());
return output.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
這是traverse.gs:
function runtraverseFolder()
{
var dirlist = loadDirlist();
var controls = '<div class="cntrls"><input type="button" value="Exit" onClick="google.script.host.close()" /> \
<input type="button" value="Delete Cached Listing & Start Over" onClick="google.script.run.clearDirlist();" /></div><br />';
var toptobody = loadFile('TopToBody');
var bodytoend = loadFile('BodyToEnd');
if(dirlist)
{
dispStatus('Stored MyDrive Directory List', toptobody + GStorage + bodytoend, 600, 500);
}
else
{
GStorage ='';
GFlag = 0;
traverseFolder(DriveApp.getFolderById('ThisistheIDoftheRootTraverseFolder'));
saveDirlist();
dispStatus('Current MyDrive Directory Listing', toptobody + GStorage + bodytoend, 600, 500);
}
}
function getStoredDirectory()
{
loadDirlist();
var toptobody = loadFile('TopToBodyWeb');
var bodytoend = loadFile('BodyToEndWeb');
return toptobody + GStorage + bodytoend;
}
function traverseFolder(folderObj)
{
glevel++;
if(glevel < 2)
{
GStorage += '<li class="fldr" id="fd' + glii++ + '"' + 'data-jstree=\'{ "selected" : true, "opened" : true }\'' + '><a href="https://drive.google.com/open?id='+ folderObj.getId() +'" target="_blank" >Folder: ' + folderObj.getName() + '</a></span>';
//GStorage += '<li class="fldr" id="fd' + glii++ + '"' + 'data-jstree=\'{ "selected" : true, "opened" : true }\'' + '><a href="#" target="_blank" title="Dummy Link can not get file listing" >Folder: ' + folderObj.getName() + '</a></span>';
}
else
{
GStorage += '<li class="fldr" id="fd' + glii++ + '"><a href="https://drive.google.com/open?id=' + folderObj.getId() +'" target="_blank" >Folder: ' + folderObj.getName() + '</a></span>';
//GStorage += '<li class="fldr" id="fd' + glii++ + '"><a href="#" target="_blank" title="Dummy Link can not get file listing." >Folder: ' + folderObj.getName() + '</a></span>';
}
GStorage += '<ul id="ul'+ guli++ +'">';
var subs = folderObj.getFolders();
var files = folderObj.getFiles();
if(files)
{
//GStorage += '<ul id="ul'+ guli++ +'">';
while(files.hasNext())
{
var fi = files.next();;
GStorage += '<li class="file" id="fi' + glii++ + '"><a href="https://drive.google.com/open?id='+ fi.getId() +'" target="_blank" title="Right Click to Open File in new tab." >File: ' + fi.getName() + '</a></span>';
}
//GStorage += '</ul>';
}
while (subs.hasNext())
{
traverseFolder(subs.next());
}
GStorage += '</ul></li>';
glevel--;
}
function saveDirlist()
{
var filename = 'CurrentDirectoryListing';
var fldr = DriveApp.getFolderById(folderID);
var fldrname = fldr.getName();
var file = fldr.getFilesByName(filename);
var targetFound = false;
var timeStamp = '<div class="timestmp">' + Utilities.formatDate(new Date(), "GMT-7", "yyyy-MM-dd HH:mm:ss") + '</div>';
while(file.hasNext())
{
var fi = file.next();
var target = fi.getName();
if(target == filename)
{
targetFound = true;
fi.setContent(timeStamp + GStorage);
SpreadsheetApp.getUi().alert('Directory Listing was updated using: ' + target); }
}
if(!targetFound)
{
var created = fldr.createFile('CurrentDirectoryListing',timeStamp + GStorage);
if(created)
{
//SpreadsheetApp.getUi().alert('Directory Listing was stored here: ' + fldr.getName() + '/' + created.getName());
}
else
{
SpreadsheetApp.getUi().alert('Unknown Error: Directory List was not stored');
}
}
}
function loadDirlist()
{
var filename = 'CurrentDirectoryListing';
var fldr = DriveApp.getFolderById(folderID);
var file = fldr.getFilesByName(filename);
var targetFound = false;
while(file.hasNext())
{
var fi = file.next();
var target = fi.getName();
if(target == filename)
{
targetFound = true;
GStorage = fi.getBlob().getDataAsString();
//SpreadsheetApp.getUi().alert('Directory Listing was retreived from: ' + fldr.getName() + '/' + target);
}
}
return targetFound;
}
function clearDirlist()
{
var filename = 'CurrentDirectoryListing';
var fldr = DriveApp.getFolderById(folderID)
var file = fldr.getFilesByName(filename);
var targetFound = false;
while(file.hasNext())
{
var fi = file.next();
var target = fi.getName();
if(target == filename)
{
targetFound = true;
fldr.removeFile(fi);
//SpreadsheetApp.getUi().alert('File: ' + filename + ' was removed from: ' + fldr.getName() + '/' + target);
}
}
return targetFound;
}
This is utility.gs:
function dispStatus(title,html,width,height)
{
// Display a modeless dialog box with custom HtmlService content.
var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
var width = typeof(width) !== 'undefined' ? width : 250;
var height = typeof(height) !== 'undefined' ? height : 300;
var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
var htmlOutput = HtmlService
.createHtmlOutput(html)
.setWidth(width)
.setHeight(height);
SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
}
的腳本對我的作品。您是否爲腳本提供了驅動器授權? – Ritz
我有。今天上午它似乎對我更好。也許Google雲端硬盤需要清空或其他內容。 – Databoy2k