I've generated QRcode by using image function (=image("https://chart.googleapis.com/....) and want to place in google doc, after the export to PDF, but it's show nothing, How do i place QRcode from spreadsheet in doc as image. Thank everyone for advance.
Here is code. (Just Example)
function createBulkPDFs(e) {
const pdfFolder = DriveApp.getFolderById("xxx");
const tempFolder = DriveApp.getFolderById("xxx");
const docFile = DriveApp.getFileById("xxx");
const ws = SpreadsheetApp.openById("xxx").getSheetByName("eee");
const range = ws.getRange(2, 1, ws.getLastRow() - 1, 6).getValues();
var data = range[range.length - 1];
createPDF(data[1],data[3], data[4], data[5], data[6], data[3] + "" + data[4], docFile, tempFolder, pdfFolder);
}
function createPDF(qcode, First, Last, address, quantity, pdfName, docFile, tempFolder, pdfFolder) {
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
body.replaceText("{qr}", qcode);
body.replaceText("{fn}", First);
body.replaceText("{ln}", Last);
body.replaceText("{addr}", address);
body.replaceText("{qty}", quantity);
tempDocFile.saveAndClose();
const pdfContentBolb = tempFile.getAs("application/pdf");
const pdfFile = pdfFolder.createFile(pdfContentBolb).setName(pdfName);
tempFile.setTrashed(true);
return pdfFile;}
Just want to place B column as image and save to PDF. and google sheet here !! Click...
I believe your goal as follows.
createPDF
like createPDF(data[1],data[3], data[4], data[5], data[6], data[3] + "" + data[4], docFile, tempFolder, pdfFolder)
.
data[6]
is used. In this case, an error occurs at body.replaceText("{qty}", quantity)
.createPDF(qcode, First, Last, address, quantity, pdfName, docFile, tempFolder, pdfFolder)
, I thought that it is required to be createPDF(data[1], data[2], data[3], data[4], data[5], data[2] + "" + data[3], docFile, tempFolder, pdfFolder)
.=image("https://chart.googleapis.com/....
, I would like to propose to retrieve the image blob using UrlFetchApp. About the script for replacing the text to image, I used my sample script at this answer.
data
you retrieved.When above points are reflected to your script, it becomes as follows.
Before you use this script, please set each values of xxx
.
function createBuikPDFs(e) {
const pdfFolder = DriveApp.getFolderById("xxx");
const tempFolder = DriveApp.getFolderById("xxx");
const docFile = DriveApp.getFileById("xxx");
const ws = SpreadsheetApp.openById("xxx").getSheetByName("eee");
const values = ws.getRange(2, 1, ws.getLastRow() - 1, 6).getValues();
const length = values.length;
var data = values[length - 1];
const formula = ws.getRange(length + 1, 2).getFormula();
const url = formula.replace(/\=image\("/i, "").replace(/chl\="&.+/i, `chl=${data[5]}`);
data[1] = UrlFetchApp.fetch(url).getBlob();
createPDF(data[1], data[2], data[3], data[4], data[5], data[2] + "" + data[3], docFile, tempFolder, pdfFolder);
}
function createPDF(qcode, First, Last, address, quantity, pdfName, docFile, tempFolder, pdfFolder) {
// This function is from https://stackoverflow.com/a/51913863
var replaceTextToImage = function (body, searchText, image, width) {
var next = body.findText(searchText);
if (!next) return;
var r = next.getElement();
r.asText().setText("");
var img = r.getParent().asParagraph().insertInlineImage(0, image);
if (width && typeof width == "number") {
var w = img.getWidth();
var h = img.getHeight();
img.setWidth(width);
img.setHeight(width * h / w);
}
return next;
};
const tempFile = docFile.makeCopy(tempFolder);
const tempDocFile = DocumentApp.openById(tempFile.getId());
const body = tempDocFile.getBody();
replaceTextToImage(body, "{qr}", qcode);
body.replaceText("{fn}", First);
body.replaceText("{ln}", Last);
body.replaceText("{addr}", address);
body.replaceText("{qty}", quantity);
tempDocFile.saveAndClose();
const pdfContentBolb = tempFile.getAs("application/pdf");
const pdfFile = pdfFolder.createFile(pdfContentBolb).setName(pdfName);
tempFile.setTrashed(true);
return pdfFile;
}