Upload files to GDrive followed by a link of the uploaded file in Google sheet

I created an app that at the same time sends data to a google sheet and image to a drive where the uploaded image URL is displayed alongside the data sent. My issue is that the image is uploaded properly with the date and time in the title but my data aren't sent to the google script.

(by the way, I inspired my project from METRICAT https://ai2.metricrat.co.uk/guides/upload-any-file-to-google-drive-with-ai2)

I tried but didn't found any workable solution.

here are my blocks

here is my code

function doGet(e) {
    SpreadsheetApp.openById('XXXXXXXXXX').getSheetByName("Test_Sheet");
     return addUser(e);
   }
   
   function doPost(e) {
    SpreadsheetApp.openById('XXXXXXXXXX').getSheetByName("Test_Sheet");
     return addUser(e);
   }
   
   function addUser(e) {
   
     var tag1 = e.parameter.tag1;
     var tag2 = e.parameter.tag2;
     var name = e.parameter.name;
     var mimetype = e.parameter.mimetype;
     var data = e.parameter.data;
     var filename = name + Utilities.formatDate(new Date(), "GMT+4", " dd-MM-yyyy HH:mm");
   
     var data = Utilities.base64Decode(data);
     var blob = Utilities.newBlob(data, mimetype, filename);
     var file = DriveApp.getFolderById('XXXXXXXXX').createFile(blob).getID();
   
     file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
     var fileId = file.getId();
   
     var fileUrl = "https://drive.google.com/uc?export=view&id=" + fileId;
   
     var sheet = SpreadsheetApp.openById('XXXXXXXXXX').getSheetByName("Test_Sheet");
     sheet.appendRow([tag1, tag2, name, fileUrl])
   
     return ContentService.createTextOutput("Data uploaded");
   
   }

Your problem is here:

var file = DriveApp.getFolderById('XXXXXXXXX').createFile(blob).getID();
   
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

var fileId = file.getId();

You are setting the variable file to the fileId(a string) instead of a file object, plus a typo on getId().

Change to:

var fileID = DriveApp.getFolderById('XXXXXXXXX').createFile(blob).getId();

var file = DriveApp.getFileById(fileID);

file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

var fileUrl = "https://drive.google.com/uc?export=view&id=" + fileID;

not tested.....

changed done but now neither image nor data are uploaded.

The only real way to prove this is to offer you a working demo, so here we go:

BLOCKS - uses the very latest version of KIO4_Base641 extension, this allows for working with any Android version both before and after 10 (for now...). Just a single image in the assets to test with.
http://kio4.com/appinventor/277_extension_imagen_string.htm

SCREEN - shows response after submission

image

SCRIPT - no need for the doGet(e) as we are using POST. I used the filename, as opposed to the name, to send to the sheet, and removed any spaces / difficult characters from the filename.

function doPost(e) {
    var ss = SpreadsheetApp.openById('1OwQb6sxYa-eyHWje_fVTnBjDqFapN7KxRcMwkZnnsdM');
    var sh = ss.getSheetByName('Test_Sheet');
      
     var tag1 = e.parameter.tag1;
     var tag2 = e.parameter.tag2;
     var name = e.parameter.name;
     var mimetype = e.parameter.mimetype;
     var data = e.parameter.data;
     var filename = name + "_" + Utilities.formatDate(new Date(), "GMT+4", "dd-MM-yyyy-HH-mm");
   
     var data = Utilities.base64Decode(data);
     var blob = Utilities.newBlob(data, mimetype, filename);
     var fileID = DriveApp.getFolderById('1WeowpWfFpY7Auw40uwZEx5U1z4s8vvJ9').createFile(blob).getId();
     var file = DriveApp.getFileById(fileID);

     file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);

     var fileUrl = "https://drive.google.com/uc?export=view&id=" + fileID;
   
     sh.appendRow([tag1,tag2,filename,fileUrl])
   
     return ContentService.createTextOutput("Data uploaded");
   
   }

SHEET - you can see three entries from three image submissions

FOLDER - the "people" icon shows on the the three uploaded files, which indicates sharing. Note that the google sheet is NOT shared.

image

AIA

DemoFileURLAndData.aia (50.6 KB)

@TIMAI2, first of all, thank you very much for the demo. I wanted to add pictures from the camera and try to upload them but I got errors. I modified the path and still errors.

my blocks

when adding camera

after modifying path

Thanks for withholding that little nugget of information :imp:

You problem is now in your blocks that you have shown and with file paths to the camera image.

I will need to do more work to show you what you need.

grafik

grafik

@Anke

I have asked @Juan_Antonio about this, the // does not work :wink:

@Jean_Yves_Le_Magnifi

Re: Using camera for image

Try these blocks, they are working for me:

You may want to introduce an image resizing routine using Taifun's Image extension (or other method). camera images can be between 2 - 5 mb in size, these will take a long time to upload, and of course take up more space/storage on your google drive account. If you need the full size, then perhaps introduce a progress dialog to show that something is happening....

Ok, but this shouldn't work for the compiled app (APK).

You are right. This ?

image

Otherwise file extension required to get pathToAssets and copy file to ASD.....

[edit - to be honest, I was focusing on the proof for the google apps script web app, more than the blocks]

@TIMAI2, apologize for the lateness of response, first of all, thank you very much I tested with multiples tags alongside and it works perfectly. (i thought about the Taifun extension for the Pictures size).
Last but not least I tried with multiple pictures and my issue is that when uploading the picture, the last one appeared every time (by the way I'm not trying to upload all at the same time).

every image has its own button which calls the same blocks to upload

display of the app:

Here are my blocks:
(i tried to set the image to global but I got errors)

thanks in advance :slight_smile:

Possibly here:

image

Your filename needs to be for the image related to the button. Set a variable for image in the sendPic procedure, and set this in the button longClick event - imagePicture...

@TIMAI2, the created variable still uploads the same picture. And when added to the button, I got error.

quite stuck here :frowning:

Something like this:

image

you may have to edit the image.Picture path

@TIMAI2, ok but that about the camImage variables present in the kio4 base .GotString and .AfterPicture

Advise applied but i still get errors while sending

its the last straight line and its the dot of the app :frowning:

@TIMAI2, for the info I got this error while sending

thanks in advance :slight_smile:

You are not sending a correctly encoded base64 string. Use Do It to check your encoded string, and trace the issue, most probably an incorrect path to the file.

@TIMAI2, if you don't mind, can you please have a look at my aia. I can't see the issue. :slight_smile:

test2.aia (48.3 KB)