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.
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");
}
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;
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
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
@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.
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....
@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
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...
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.