0
votes

I am new to Google script and I want to create a script that bring the image name from spreadsheet and insert that image in google docs. My problem is the names of images. In spreadsheet they are stored with the same text in the start 'testui/'. I need to delete it from beginning. Tried used replaceText('testui/', ''), but got message - TypeError: Cannot find function replaceText in object testui/01-03-2019 11-02-14.Signature.054223.png

    function createDocument() {
var headers = Sheets.Spreadsheets.Values.get('1M9biFRsbMtc6zDcAWgOYqJwNbivvVet-ZCD4joI', 'A1:I1');
var tactics = Sheets.Spreadsheets.Values.get('1M9biFRsbMtc6zDcAWgOYqJwNbivvVet-ZCD4joI', 'A2:I3');
var templateId = '1PxbTS67vrK8tqruNqjkqJMcjtGbqzQ';

for(var i = 0; i < tactics.values.length; i++){

var TimeStamp = tactics.values[i][0];
var AktoTipas = tactics.values[i][2];
var Kodas = tactics.values[i][6];
var Signature = tactics.values[i][8];

/*Here I got message TypeError: Cannot find function replaceText in object testui/01-03-2019 11-02-14.Signature.054223.png
Signature.replaceText('testui/', '')
*/

//Make a copy of the template file
var documentId = DriveApp.getFileById(templateId).makeCopy().getId();

//Rename the copied file
DriveApp.getFileById(documentId).setName(AktoTipas + '-' + TimeStamp + '-' + Kodas);

//Get the document body as a variable
var body = DocumentApp.openById(documentId).getBody();

//Insert the supplier name
body.replaceText('##aktas##', AktoTipas)
body.replaceText('##Laikas##', TimeStamp)
body.replaceText('##Signature##', Signature)

insertImageFromDrive(body, Signature);

}

}
function insertImageFromDrive(body, Signature){
var img = DriveApp.getFilesByName(Signature);
while (img.hasNext()) {
var image = img.next();
body.insertImage(3, image); 
}
}

If you have same ideas I will be very happy! Thank you very much.

1

1 Answers

0
votes

You are on the right track. However, replacetext() is function specific for google document body object.

The values you get from individual cells of the spreadsheet can be classified into various javascript variable as defined here. Yes, I said javascript, google script is based on javascript language and for most function pertaining to manipulating variables you follow javascript syntax and functions.

So, in this case, the Signature is a string variable and you will use string.replace() function of javascript to change or manipulate your string, as documented here

In your specific case you will do the following:

Signature = Signature.replace('testui/', '')