How to get the google drive ID of picture just being uploaded

2,581 views
Skip to first unread message

Guy Lagasse

unread,
Jul 24, 2018, 1:32:33 PM7/24/18
to MIT App Inventor Forum

Good day all,

I am using TimAI2's method to upload pictures direct to google drive, The problem i cannot figure out, is how to pull the ID of the picture being uploaded so that i can insert it into a google sheet. Currently I have two app scripts. One is to upload a picture and the other is to insert data into a google sheet. I am very green to javascript. Will i need to combine them to one app script to be able to pass a function or variable?

Here are the scripts:

Code 1
function doGet(e) {
return message("Error: no parameters in doGet");
 
}

function doPost(e) {
if (!e.parameters.filename || !e.parameters.file || !e.parameters.imageformat) {
  return message("Error: Bad parameters in doPost");
} else {
  var imgf = e.parameters.imageformat[0].toUpperCase();
  var mime =
      (imgf == 'BMP')  ? MimeType.BMP
    : (imgf == 'GIF')  ? MimeType.GIF
    : (imgf == 'JPEG') ? MimeType.JPEG
    : (imgf == 'JPG')  ? MimeType.JPEG
    : (imgf == 'PNG')  ? MimeType.PNG
    : (imgf == 'SVG')  ? MimeType.SVG
    : false;
  if (mime) {
    var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8);
    var blob = Utilities.newBlob(data, mime, e.parameters.filename);
    DriveApp.getFolderById('FOLDER ID HERE').createFile(blob);
    return message("Success");
  } else {
    return message("Error: Bad image format");
  }
}
}

function message(msg) {
return ContentService.createTextOutput(JSON.stringify({Result: msg })).setMimeType(ContentService.MimeType.JSON);
}
Code 2:
function doGet(e){
  
  var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/"sheet_ID"/edit#gid=0");
  var sheet = ss.getSheetByName("Sheet1");
  
  addReport(e,sheet);
  
}

//if you are using doPost / post methods in your client
function doPost(e){
  
  var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/"sheet_ID"/edit#gid=0");
  var sheet = ss.getSheetByName("Sheet1");
  
  addReport(e,sheet);
  
}

function addReport(e,sheet) {
  
  var equipment = e.parameter.equipment;
  var area = e.parameter.area;
  var type = e.parameter.type;
  var points = e.parameter.points;
  var location = e.parameter.location;
  var date = e.parameter.date;
  var wo = e.parameter.wo;  
  var comments = e.parameter.comments;
  var actions = e.parameter.actions;
  
  
  sheet.appendRow([equipment,area,type,points,location,date,wo,comments,actions]);
  
}


TimAI2

unread,
Jul 24, 2018, 5:43:02 PM7/24/18
to MIT App Inventor Forum
As I suggested on youtube have you tried this:

var imageID = DriveApp.getFolderById('FOLDER ID HERE').createFile(blob).getId();

This should still create the file and return the ID of the file, which you can then pass to your google sheet

(not tried it though...)

Guy Lagasse

unread,
Jul 24, 2018, 5:54:54 PM7/24/18
to MIT App Inventor Forum
Thanks again for your reply Tim. I am not sure how to reference the "imageID" variable in the second app script "code 2". Do i need to combine both pieces of codes into one app script? im sorry, i am very new to java. 

TimAI2

unread,
Jul 25, 2018, 2:32:45 PM7/25/18
to MIT App Inventor Forum
It is javascript (well google modified javascript) not java ;)

Yes, you will need to combine the code, in order to call the google sheet from the first script and add the file ID to it

Guy Lagasse

unread,
Jul 25, 2018, 2:42:29 PM7/25/18
to mitappinv...@googlegroups.com
Here is what I have tried with no avail. The other variables all get stored in the GSheet no problem, but still no fileID is posted. Here is my attempt at combining the scripts.
I've tried doing everything in the doPost function with no result so currently I have tried with keeping the addReport function, still with no ID.

I may also have reports submitted with no image attached, so would I be correct in assuming that when I can pass the file ID to the GSheet, that I can do the same process in the ELSE?
function doGet(e) {
return message("Error: no parameters in doGet");
   
}

function doPost(e) {
 if (!e.parameters.filename || !e.parameters.file || !e.parameters.imageformat) {
   return message("Error: Bad parameters in doPost");
 } else {
   var imgf = e.parameters.imageformat[0].toUpperCase();
   var mime =
       (imgf == 'BMP')  ? MimeType.BMP
     : (imgf == 'GIF')  ? MimeType.GIF
     : (imgf == 'JPEG') ? MimeType.JPEG
     : (imgf == 'JPG')  ? MimeType.JPEG
     : (imgf == 'PNG')  ? MimeType.PNG
     : (imgf == 'SVG')  ? MimeType.SVG
     : false;
   if (mime) {
     var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8);
     var blob = Utilities.newBlob(data, mime, e.parameters.filename);
     var image = DriveApp.getFolderById('FILE_ID').createFile(blob);
     var linkID = image.getId();
     
     var sheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/SHEET_ID/edit?usp=sharing").getSheetByName("Sheet1");
     
     addReport(e,sheet);
     
     return message("Success");
     
   } else {
     return message("Error: Bad image format");
   }
 }
}

function addReport(e,sheet) {
  
  var equipment = e.parameter.equipment;
  var area = e.parameter.area;
  var type = e.parameter.type;
  var points = e.parameter.points;
  var location = e.parameter.location;
  var date = e.parameter.date;
  var wo = e.parameter.wo;  
  var comments = e.parameter.comments;
  var actions = e.parameter.actions;
  var time = e.parameter.time;
  var status = e.parameter.status;
  var link = e.linkID;
   
  sheet.appendRow([time,status,equipment,area,type,points,location,date,wo,comments,actions,link]);

TimAI2

unread,
Jul 26, 2018, 6:00:53 PM7/26/18
to MIT App Inventor Forum
This works, you will just have to amend to account for the other items you want to append:

function doGet(e) {
 return message("Error: no parameters in doGet");
    
}

function doPost(e) {
  if (!e.parameters.filename || !e.parameters.file || !e.parameters.imageformat) {
    return message("Error: Bad parameters in doPost");
  } else {
    var imgf = e.parameters.imageformat[0].toUpperCase();
    var mime =
        (imgf == 'BMP')  ? MimeType.BMP
      : (imgf == 'GIF')  ? MimeType.GIF
      : (imgf == 'JPEG') ? MimeType.JPEG
      : (imgf == 'JPG')  ? MimeType.JPEG
      : (imgf == 'PNG')  ? MimeType.PNG
      : (imgf == 'SVG')  ? MimeType.SVG
      : false;
    if (mime) {
      var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8);
      var blob = Utilities.newBlob(data, mime, e.parameters.filename);
     
      //get the image file ID like this:
      var imageID = DriveApp.getFolderById('1gTB0ABCs2gpqYpBGSFx3nyTZ0QzZbtyZ').createFile(blob).getId();
     
      //append the ID to your spreadsheet like this:
      var ss = SpreadsheetApp.openById('1fDJ7_oj4JAR3l2uljghlW2rcpJYiA8rnD7uPgKc0QjI');
      var sh = ss.getSheets()[0];
      sh.appendRow([imageID]);
     
      return message("Success");
    } else {
      return message("Error: Bad image format");
    }
  }
}

Indian people

unread,
Jul 29, 2019, 1:48:59 AM7/29/19
to MIT App Inventor Forum
can u guys provide me the aia file I m working on the same kind of a project but getting a problem. needed urgent 
Reply all
Reply to author
Forward
0 new messages