Onedit replace text with Image

183 views
Skip to first unread message

Dave Meek

unread,
Aug 1, 2022, 10:56:18 AM8/1/22
to Google Apps Script Community
Hi All

I wondering if someone can help me, I have got a bit stuck in a big project.

The Project as a whole if from a google form create a PDF,  approve the doc then email it.

But along the way I'm using vlookups to find the email and logo's.

I have a formula that replaces text with a picture and this works fine sepately but as soon as i try to bring it into the project it fails with     Exception: Invalid argument: image
    at replaceTextToImage(Code:105:33).

Here is my code, I have highlighted in yellow where its failing

function afterapproval (e,sh,entryRow,info1,Logo,Incident,fname,sname,leavet,leaver,state) {
var sh1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("logs");
const entryRow1 = e.range.getRow();
const status = sh1.getRange(entryRow1, 4).getValue();

Logger.log(status)

if (status == "APPROVED")
{const nextstep = thenextstep(sh,entryRow,info1,Logo,Incident,fname,sname,leavet,leaver,state);
}
}

function thenextstep (sh,entryRow,info1,Logo,Incident,fname,sname,leavet,leaver,state) {

const pdfFile = createPDF(info1,Logo,Incident,fname,sname,leavet,leaver);



sh.getRange(entryRow, 10).setValue(pdfFile.getUrl());
sh.getRange(entryRow, 11).setValue(pdfFile.getName());
const email = sh.getRange(entryRow, 13).getValue();



//Logger.log(Logo)
sendEmail(email,pdfFile);

}


function sendEmail(email,pdfFile){

GmailApp.sendEmail(email, "Here is Your PDF", "Your PDF is attached.",{
attachments: [pdfFile],
name:'Fenix Monitoring'

});
}


function createPDF(info1,Logo,Incident,fname,sname,leavet,leaver){

const pdfFolder = DriveApp.getFolderById("1dRPwAId2gLsf6tIk_wDFetXEYAySmP93");
const tempFolder = DriveApp.getFolderById("1LcdygqkKdCbOdu7r1z3XeFFM2iyMp6rB");
const templateDoc = DriveApp.getFileById("1WzRwG_wlkMwI0JlKBvj4QCMPD3VF1IQy1r1e0Y-AgZY");

const newTempFile = templateDoc.makeCopy(tempFolder);

const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
body.replaceText("{fn}", [fname]);
body.replaceText("{ln}", [sname]);
body.replaceText("{addr}", [leavet]);
body.replaceText("{qty}", [leaver]);

var replaceTextToImage = function(body, searchText, image) {
    var next = body.findText(searchText);
    if (!next) return;
    var r = next.getElement();
    r.asText().setText("");
    r.getParent().asParagraph().insertInlineImage(0, image);

   
    return next;
  };
var replaceText = "{Incident Photo}";
  var image = Incident;

   
  do {
    var next = replaceTextToImage(body, replaceText, image, );
  } while (next);


const header = openDoc.getHeader();
var replaceTextToImage = function(header, searchText, image, width) {
    var next = header.findText(searchText);
    if (!next) return;
    var r = next.getElement();
    r.asText().setText("");
    var img = r.getParent().asParagraph().insertInlineImage(0, Logo);
    if (width && typeof width == "number") {
      var w = img.getWidth();
      var h = img.getHeight();
      img.setWidth(width);
      img.setHeight(width * h / w);
    }
    return next;
  };

    var replaceText = "{Installer Logo}";
  var image = Logo;
  do {
    var next = replaceTextToImage(header, replaceText, Logo, );
  } while (next);




openDoc.saveAndClose();

const blobPDF = newTempFile.getAs(MimeType.PDF)
const pdfFile = pdfFolder.createFile(blobPDF).setName([fname] + " " + [sname] + " " + new Date());
tempFolder.removeFile(newTempFile);

return pdfFile;
}



Dave Meek

unread,
Aug 1, 2022, 11:31:49 AM8/1/22
to Google Apps Script Community
I forgot to mention that some of my things have been declared in a function above that 

here is the first part

function autoFillGoogleDocFromForm(e,state) {
// assume it's the first sheet where the data is collected
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form responses 1");
const entryRow = e.range.getRow();
var last = sh.getLastRow();
var lastcol = sh.getLastColumn();
const info1 = e.namedValues;
const fname = sh.getRange(entryRow, 3).getValue();
const sname = sh.getRange(entryRow, 4).getValue();
const leavet = sh.getRange(entryRow, 7).getValue();
const leaver = sh.getRange(entryRow, 8).getValue();
const URL = sh.getRange(entryRow, 12).getValue();
const Logo = UrlFetchApp.fetch(URL).getBlob();
const URL1 = sh.getRange(entryRow, 14).getValue();
const Incident = UrlFetchApp.fetch(URL1).getBlob();
var Requesteremail = sh.getRange(entryRow, 2).getValue();
var RequestContent = sh.getRange(entryRow, 3).getValue();

// Generate Unique ID
var Uuid = uuid_(lastcol);
// return the URL : apps needs to be deployed
var scriptUri = DEPLOY_ID;
Logger.log('uri: '+scriptUri);
// Append results in the Google Sheet
var array = [ [Uuid, "NA", "NA",
'=HYPERLINK("'+scriptUri+'?i="&ROW()&"&state=APPROVED&last="&ROW(),"Approve")', 
'=HYPERLINK("'+scriptUri+'?i="&ROW()&"&state=DENIED&last="&ROW(),"Deny")' ] ]
Logger.log('array to be inserted in the Sheet last: '+array);
// insert colum 4
var newRange = sh.getRange(last,4,1,5);
Logger.log(newRange.getA1Notation());
newRange.setValues(array);
reviewContent_(
  Requesteremail,
  RequestContent,
  Uuid,
  last
  ); // TemplateEmail + Recipients settings 

   
  //const nextstep = thenextstep(sh,entryRow,info1,Logo,Incident,fname,sname,leavet,leaver,state);
  
}


Dave Meek

unread,
Aug 3, 2022, 5:04:13 AM8/3/22
to Google Apps Script Community
I have fixed this now, this can be deleted
Reply all
Reply to author
Forward
0 new messages