'in drive Add-On (right sidebar) : Get current folder and selected Files
I'm currently working on a DRIVE Add-on.
I want to add a function to pick a file -> change the name -> move to a selected folder.
Do you know a way to get the current folder and selected files in my Add-on ???
Solution 1:[1]
Accessing the current selected item
You can use the manifest to specify the following:
"drive": {
"homepageTrigger": {
"runFunction": "onHomePage",
"enabled": true
},
"onItemsSelectedTrigger": {
"runFunction": "funcname"
}
}
The event object passed to the function
function funcname(e) {
//e contains an event object
Sample Event Object:
{
"commonEventObject": { ... },
"drive": {
"activeCursorItem":{
"addonHasFileScopePermission": true,
"id":"0B_xxxxx",
"iconUrl": "https://drive-thirdparty.googleusercontent.com...",
"mimeType":"application/pdf",
"title":"How to get started with Drive"
},
"selectedItems": [
{
"addonHasFileScopePermission": true,
"id":"0B_xxxxxx",
"iconUrl":"https://drive-thirdparty.googleusercontent.com...",
"mimeType":"application/pdf",
"title":"How to get started with Drive"
},
...
]
},
...
}
e.selectedItems.id contains the id of the selected item
If a file is selected you can get the id and the file and the parents will provide the current folder. The mimeType can be used to distinguish between files and folders
A simple drive addon to provide ids and mimeTypes of selected items
function onHomePage(e) {
//Logger.log(JSON.stringify(e));
var cards = [];
cards.push(displayFileId(e));
return cards;
}
function displayFileId(e) {
var card = CardService.newCardBuilder();
card.setHeader(CardService.newCardHeader().setTitle('File Information'));
var section = CardService.newCardSection();
var idtxt = CardService.newTextParagraph().setText('Click on a file to get file id');
section.addWidget(idtxt);
card.addSection(section);
return card.build();
}
function onMyDriveItemSelected(e) {
var s = '';
if (e.drive.selectedItems.length > 0) {
e.drive.selectedItems.forEach(function(itm,i) {
if (i > 0) { s += '<br><br>'; }
s += Utilities.formatString(' %s<br>%s<br> ', itm.id,itm.mimeType);
});
}
var card = CardService.newCardBuilder();
var section = CardService.newCardSection();
var infotxt = CardService.newTextParagraph().setText(s);
section.addWidget(infotxt);
card.addSection(section);
return card.build();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
