'Trying to retrieve Google File ID for an image in google sheets
I have an Appsheet app, for which I am trying to get the Google File ID of an image that I have taken through the app.
When the image is taken in the app, it is stored in the associated Sheets file by its name, which looks like this:
Expenses_Images/1.Receipt Image.111508.jpg
I would like to use an Appscript to find the relevant image in Google drive and show the file ID as a result in the following column. The sheet would look something like this:
Expense Image File ID
Expenses_Images/1.Receipt Image.111508.jpg......... XXXXXXXXXX
Where XXXXXX
is the file ID i'm trying to retrieve and Expense
, Image
and File ID
are the column headers. I would like the appscript to automatically find the File ID
whenever a new row is added to the spreadsheet.
I have the following appscript, but I'm not sure it is going to achieve exactly what I want.
function list_all_files_inside_one_folder_without_subfolders(){
var sh = SpreadsheetApp.getActiveSheet();
var folder = DriveApp.getFolderById(‘ChangeHereToYourFileFolderId’);
var list = ;
list.push([‘Name’,‘ID’,‘Size’]);
var files = folder.getFiles();
while (files.hasNext()){
file = files.next();
var row = row.push(file.getName(),file.getId(),file.getSize())
list.push(row);
}
sh.getRange(1,1,list.length,list[0].length).setValues(list);
}
Solution 1:[1]
To retrieve a fileID from the name of the file ...
function getFileId(fileName) {
var files = DriveApp.getFilesByName(fileName);
while (files.hasNext()) {
var file = files.next();
return (file.getId())
}
}
You can call this function this ay for instance
function test(){
var sh=SpreadsheetApp.getActiveSheet()
for (var i =1;i<=sh.getLastRow();i++){
id = getFileId(sh.getRange(i,1).getValue())
try {sh.getRange(i,2).setValue(id)}
catch(e){sh.getRange(i,2).setValue(e)}
}
}
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 |