function setImage(po) {
try{
var image,sh,ss;
/*
po.blobSource - a blob that is an image file type
po.column - the column to set the image in
po.functionName - the name of the function to assign to the image
po.row - the row to set the image in
po.shName - the name of the sheet tab
*/
ss = SpreadsheetApp.getActiveSpreadsheet();//This code is bound to a Sheet
sh = ss.getSheetByName(po.shName);
image = sh.insertImage(po.blobSource, po.column, po.row);//Insert an image and return the image
image.assignScript(po.functionName);//Assign an Apps Script function to the image
return true;
}catch(e){
Logger.log('Error: ' + e.message)
Logger.log('stack: ' + e.stack)
return false;
}
}
function testRun() {
var blobSrc,contentType,file,files;
/*
In a Sheet click Insert and Drawing
Draw a button
Save the button to your computer drive
From Google Drive click "New" and File Upload and upload the file and give it a name
Get the image file in code as shown in this function
Convert the image file to a blob
*/
files = DriveApp.searchFiles('mimeType contains "jpeg" and title contains "Orange Diagnal"');
if (files.hasNext()) {
file = files.next();
}
contentType = 'image/jpeg';
if (file) {
blobSrc = file.getAs(contentType);
}
var blobStr = blobSrc.getDataAsString();
blobStr = Utilities.base64Encode(blobStr);
//Logger.log(Array.isArray(bytes))
//DriveApp.createFile('bytes of orange', blobStr, MimeType.PLAIN_TEXT)
//Logger.log('blobStr: ' + blobStr)
//Logger.log(blobSrc.getContentType());
blobStr = Utilities.base64Decode(blobStr);
blobSrc = Utilities.newBlob(blobStr, contentType).getAs(contentType);
Logger.log(blobSrc.getContentType())
setImage({shName:'Update File',blobSource:blobSrc,row:37,column:1,functionName:'myFunction'});
}