我需要转换这个 UNC 的路径。我搜索了又搜索,无法拼凑任何东西。
"\\NAS_01\GlobalShare\Docs\Customers\2017\S\Smith, John\photo1.jpg"
我需要删除路径的“\NAS_01\GlobalShare\Docs\Customers\”部分以及“photo1.jpg”并以:
2017\S\Smith, John\
这样我就可以将它传递给以下函数:
function getDriveFolderNoCreate(path, rootFolder) {
var name, folder, search, fullpath;
// Remove extra slashes and trim the path
fullpath = path.replace(/^\/*|\/*$/g, '').replace(/^\s*|\s*$/g, '').split("/");
// Always start with the main Drive folder
folder = rootFolder;
for (var subfolder in fullpath) {
name = fullpath[subfolder];
search = folder.getFoldersByName(name);
if (search.hasNext()) {
var folder = search.next;
var folderID = folder.getId();
return folderID;
}
}
}
我的目的是返回一个 url 以打开具有相同路径的 Google Drive 文件夹。
最佳答案
我最终得到了一个非常有效的多部分解决方案。
我将填充 UNC 路径粘贴到单元格 B2。 这个公式在B3 =Index(split(B2, "\"), 0, 8) 它返回我需要的确切文件夹名称。 然后在我的 gs 文件中:
function findDriveFolder() {
var pFId = "XYZ1233333333333";
var input = SpreadsheetApp.getActive().getRange("B3").getValue();
var folders = DriveApp.getFoldersByName(input);
Logger.log("folders: " + folders[0]);
while (folders.hasNext()) {
var folder = folders.next();
var url = folder.getUrl();
showAnchor("View Folder", url);
}
}
function showAnchor(name,url) {
var html = '<html><body><a href="'+url+'" target="blank" onclick="google.script.host.close()">'+name+'</a></body></html>';
var ui = HtmlService.createHtmlOutput(html)
SpreadsheetApp.getUi().showModelessDialog(ui,"Files Folder");
}
我还没有实现 searchFolders 部分,我希望它能加快速度。至少它现在有效。
关于javascript - 将路径字符串从 unc 转换为 uri,在 Google Apps 脚本中用反斜杠替换斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50707963/