Berikut ini adalah implementasi kedua skrip yang lebih lengkap dan andal dalam jawaban ini ( pertama dan kedua ) untuk pertanyaan ini. Script ini memungkinkan Anda untuk menempel URL lengkap file atau ID file ke dalam kotak edit pertama dalam dialog dan kemudian mengembalikan representasi teks dari path serta tautan ke direktori induk. Instruksi instalasi identik dengan apa yang ditunjukkan di atas oleh Jacob , saya menyalinnya di bawah ini untuk kelengkapan.
CATATAN: Beberapa API yang digunakan dalam semua skrip ini sekarang sudah usang. Mereka masih bekerja pada saat posting ini dibuat, tetapi mungkin akan berhenti bekerja di masa depan.
//
// Take a Google Drive file URL or ID and output a string representation of the path as well as a link
// to the parent folder
//
// Based on https://webapps.stackexchange.com/questions/43881/how-to-view-the-parent-folder-of-a-google-document
function doGet() {
// create app and grid
var app = UiApp.createApplication();
var grid = app.createGrid(5,2);
// set labels for first column
grid.setWidget(1, 0, app.createLabel("URL or file ID: ").setStyleAttribute('fontWeight', 'bold'));
grid.setWidget(2, 0, app.createLabel("Folder Path: ").setStyleAttribute('fontWeight', 'bold'));
grid.setWidget(3, 0, app.createLabel("Folder URL: ").setStyleAttribute('fontWeight', 'bold'));
// set text boxes for second column
grid.setWidget(1, 1, app.createTextBox().setId("key").setName("key").setWidth(1000));
grid.setWidget(2, 1, app.createTextBox().setId("path").setName("path").setWidth(1000));
grid.setWidget(3, 1, app.createAnchor("","").setId("url").setName("url").setWidth(1000));
// create button and handler
grid.setWidget(4, 0, app.createSubmitButton("Find")
.addClickHandler(app.createServerHandler("listParentFolders")
.addCallbackElement(grid)));
// add grid to application and show app
app.add(grid);
return app;
}
//
// getIdFromUrl - Get the file id portion of the url. If the file id itself is passed in it will match as well
//
// From http://stackoverflow.com/questions/16840038/easiest-way-to-get-file-id-from-url-on-google-apps-script
// This regex works for any google url I've tried: Drive url for folders and files, Fusion Tables, Spreadsheets,
// Docs, Presentations, etc. It just looks for anything in a string that "looks like" a Google key. That is, any
// big enough string that has only (google key) valid characters in it.
//
// Also, it works even if it receives the ID directly, instead of the URL. Which is useful when you're asking
// the link from the user, as some may paste the id directly instead of the url and it still works.
function getIdFromUrl(url) {
return url.match(/[-\w]{25,}/);
}
function listParentFolders(e) {
var app = UiApp.createApplication();
var key = getIdFromUrl(e.parameter.key);
var theFile = DriveApp.getFileById(key);
var parents = theFile.getParents();
var fileName = theFile.getName();
// no folders
if ( parents == null ) {
app.getElementById("path").setValue('Unknown file');
return app;
}
var url;
var folder;
var folderName;
var path;
// traverse the list of parents of folders to build the path
while (parents.hasNext()){
folder = parents.next();
folderName = folder.getName();
// on the first pass get the URL of the folder which is the parent folder of the file
if (url == null)
url = folder.getUrl();
// build up a string version of the path
if (path == null)
path = folderName;
else
path = folderName + ' / ' + path;
// get the parent of the current folder
parents = folder.getParents();
}
app.getElementById("path").setValue(path);
app.getElementById("url").setHref(url).setText(url);
return app;
}
Ini terlihat seperti ini:
Untuk memasang:
- goto script.google.com , saat masuk
- mulai skrip baru
- rekatkan kode dan tekan ikon bug. Tekan otorisasi.
- dalam file> versi menaged simpan skrip.
- goto Terbitkan> Sebarkan sebagai aplikasi web dan tekan pembaruan
- url yang disajikan (exec di akhir) akan memungkinkan Anda untuk menjalankan skrip stand-a-lone.