2017-07-27 138 views
1

我絕對在代碼問題中游泳,似乎無法使其工作。我希望這組聰明的Google Apps Script-geniuses可以提供幫助。從Google表格的內容鏈接到Google雲端硬盤

第一:我的谷歌機使用以下的文件夾結構(與字母表中的每個字母的子文件夾和每個此事,然後第二子文件夾,例如

/Clients 
/A 
    /Albert, Bob (1-15-0003) Re Matter 
/B 
    /Bork, Mat (1-54-0003) Re Other Matter 

我有各種紙張的谷歌表格文檔每個引用在他們各自的單元格G2中的不同的問題編號我想要一個腳本,它會自動搜索我的Google Drive中的第二個子文件夾,並創建一個指向該文件夾的超鏈接

我的代碼如下所示,但令人遺憾的是,看起來它找不到子文件夾。

function SearchFolder() { 
// Searches Google Drive for the folder for the Active Sheet 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var sheet = ss.getActiveSheet(); 
    var searchTerm = ss.getRange('G2').getValue(); 
    var Folders = DriveApp.searchFolders("title contains '"+searchTerm.replace("'","\'")+"' and trashed = false and hidden = false"); 
    var Folder = Folders.next(); 
sheet.getRange('G2').setFormula("=HYPERLINK(\""+Folder.getUrl()+"\",\""+searchTerm+"\")"); 
} 

預期的結果將是,如果我的電流片的G2讀取1-15-0003爲G2與與谷歌雲端硬盤網址指向了/ A /阿爾伯特,鮑勃的文件夾ID的超鏈接被替換(1-15-0003)重要事項。

感謝您的任何幫助。

+0

的腳本對我的作品。您是否爲腳本提供了驅動器授權? – Ritz

+0

我有。今天上午它似乎對我更好。也許Google雲端硬盤需要清空或其他內容。 – Databoy2k

回答

1

交互式目錄樹

這些功能與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();" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <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();" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<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()" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\ 
    <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); 
} 
+0

你把絕對的工作放在這個評論中,我感謝你。它比我所尋找的代碼要多得多,但它的內容非常豐富。可惜GAS需要這麼多...... je ne sais quois ......讓它看起來很簡單。 – Databoy2k

+0

請記住,這是一個交互式解決方案。這是一個[小演示](http://www.wiechman4lakewood.com/resources/city-council-archives)。您需要在服務上使用google apps腳本,並在瀏覽器上使用javascript/html頁面,因此對於某種類型的Web應用程序而言,這是最低限度的。 – Cooper

相關問題