I tried multiple ways, but for a few days, none worked for me.
This is my script. I manage to create the doc and also the pdf. I inputted the document link but what I want is a pdf link. I couldn't get the getUrl function right for my pdf created.
Can anyone just let me know what I should put in? Many thanks in advance.
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Create Form');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('1wROa5kWXGvsOSaeb_34ncF_vcbWA4SFXGuXkwCqjAW0');
const destinationFolder = DriveApp.getFolderById('1qld3qQDQNtaGdoOxQCsSre1VjWQ6NKGn')
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Sheet1')
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index){
if (index === 0) return;
if (row[23]) return;
const copy = googleDocTemplate.makeCopy(`${row[1]} - ${row[2]} Order Form` , destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const friendlyDate = new Date(row[18]).toLocaleDateString();
body.replaceText('{{Submission Date}}', row[0]);
body.replaceText('{{Case ID}}', row[1]);
body.replaceText('{{Name}}', row[2]);
body.replaceText('{{Contact Number}}', row[3]);
body.replaceText('{{Main Service}}', row[4]);
body.replaceText('{{Type}}', row[5]);
body.replaceText('{{Brand}}', row[6]);
body.replaceText('{{Model}}', row[7]);
body.replaceText('{{IMEI No. Or Serial No.}}', row[8]);
body.replaceText('{{Warranty}}', row[9]);
body.replaceText('{{Password/Pattern}}', row[10]);
body.replaceText('{{Format}}', row[11]);
body.replaceText('{{Include Parts}}', row[12]);
body.replaceText('{{Issues}}', row[13]);
body.replaceText('{{Full Address}}', row[14]);
body.replaceText('{{Choose One}}', row[15]);
body.replaceText('{{Details}}', row[16]);
body.replaceText('{{Status}}', row[17]);
body.replaceText('{{Collection Date}}', friendlyDate);
body.replaceText('{{Special Case Reject Reason}}', row[19]);
body.replaceText('{{Quotation}}', row[20]);
body.replaceText('{{Collection Date}}', row[21]);
body.replaceText('{{Installation Date}}', row[22]);
doc.saveAndClose();
const pdfContentBlob = doc.getAs(MimeType.PDF);
DriveApp.createFile(pdfContentBlob).setName(`${row[1]} - ${row[2]} Order Form`);
const url = doc.getUrl();
sheet.getRange(index + 1, 24).setValue(url)
})}
when I put in pdfContentBlob there's no geturl function. I am not good at this at all. I hope to have someone able to give me the line of codes I just need to put in to make it work.
Thanks!
In your script, const url = doc.getUrl(); is used as the URL. In this case, the URL is for Google Document. When you want to retrieve the URL of the PDF file, how about the following modification?
From:
DriveApp.createFile(pdfContentBlob).setName(`${row[1]} - ${row[2]} Order Form`);
const url = doc.getUrl();
To:
const url = DriveApp.createFile(pdfContentBlob).setName(`${row[1]} - ${row[2]} Order Form`).getUrl();
If you want to save the PDF file to the folder of destinationFolder, please modify it as follows.
const url = destinationFolder.createFile(pdfContentBlob).setName(`${row[1]} - ${row[2]} Order Form`).getUrl();
Note:
In your script, setValue is used in a loop. In this case, the process cost becomes a bit high. If you want to reduce the process cost, I think that the following modification might be able to be used.
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('###'); // Please set your document ID.
const destinationFolder = DriveApp.getFolderById('###'); // Please set your folder ID.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1')
const [, ...rows] = sheet.getDataRange().getValues();
const values = rows.map(function (row) {
if (row[23]) return [row[23]];
console.log("Passed1")
const copy = googleDocTemplate.makeCopy(`${row[1]} - ${row[2]} Order Form`, destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const friendlyDate = new Date(row[18]).toLocaleDateString();
body.replaceText('{{Submission Date}}', row[0]);
body.replaceText('{{Case ID}}', row[1]);
body.replaceText('{{Name}}', row[2]);
body.replaceText('{{Contact Number}}', row[3]);
body.replaceText('{{Main Service}}', row[4]);
body.replaceText('{{Type}}', row[5]);
body.replaceText('{{Brand}}', row[6]);
body.replaceText('{{Model}}', row[7]);
body.replaceText('{{IMEI No. Or Serial No.}}', row[8]);
body.replaceText('{{Warranty}}', row[9]);
body.replaceText('{{Password/Pattern}}', row[10]);
body.replaceText('{{Format}}', row[11]);
body.replaceText('{{Include Parts}}', row[12]);
body.replaceText('{{Issues}}', row[13]);
body.replaceText('{{Full Address}}', row[14]);
body.replaceText('{{Choose One}}', row[15]);
body.replaceText('{{Details}}', row[16]);
body.replaceText('{{Status}}', row[17]);
body.replaceText('{{Collection Date}}', friendlyDate);
body.replaceText('{{Special Case Reject Reason}}', row[19]);
body.replaceText('{{Quotation}}', row[20]);
body.replaceText('{{Collection Date}}', row[21]);
body.replaceText('{{Installation Date}}', row[22]);
doc.saveAndClose();
const pdfContentBlob = doc.getAs(MimeType.PDF);
const url = DriveApp.createFile(pdfContentBlob).setName(`${row[1]} - ${row[2]} Order Form`).getUrl(); // or const url = destinationFolder.createFile(pdfContentBlob).setName(`${row[1]} - ${row[2]} Order Form`).getUrl();
return [url];
});
sheet.getRange(2, 24, values.length).setValues(values);
}
Related
Below is the script I am using. It creates documents as forms are submitted. The script works but every time it runs it rewrites every single form. I only want it to write new documents as forms are submitted.
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
const googleDocTemplate =
DriveApp.getFileById('');
const destinationFolder = DriveApp.getFolderById('')
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RAW');
const rows = sheet.getDataRange().getValues();
rows.forEach(function (row, index) {
if (index === 0) return;
if (row[52]) return;
const copy = googleDocTemplate.makeCopy(`${row[2]}, ${row[0]} Employee Details`,
destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const friendlyDate = new Date(row[4]).toLocaleDateString();
body.replaceText('{Q1}', row[2]);
body.replaceText('{Q2}', row[3]);
I'm trying to automate filling up tables in doc with data from sheets. I created code which creates doc for each row. Now I'd like to upgrade it a bit, so it creates only one file and appends a new table for each row from the sheet. I'm struggling with looping through the data and adding data to the same file.
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('Wygeneruj tabele');
menu.addItem('Nowa tabela', 'createNewGoogleDocs');
menu.addToUi();
}
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('file id');
const destinationFolder = DriveApp.getFolderById('folder id');
const sheet = SpreadsheetApp.getActiveSpreadsheet();
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index) {
if (index === 0) return;
if (index === 1) return;
if (row[34]) return;
const ss = sheet.getRange('A3').getValue();
const copy = googleDocTemplate.makeCopy((ss), destinationFolder);
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
body.replaceText('{{Name}}', row[1]);
body.replaceText('{{Date}}', row[2]);
body.replaceText('{{Data}}', row[3]);
rows.forEach(function(row,index) {
let final = copy.appendRow(body)
})
doc.saveAndClose();
const url = doc.getUrl();
sheet.getRange('AI3').setValue(url);
})
}
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.
You want to copy the values from Google Spreadsheet to Google Document using Google Apps Script.
The Google Spreadsheet is as follows.
Modification points:
When I saw your sample Spreadsheet and your script, it seems that you send the arguments to the function createPDF like createPDF(data[1],data[3], data[4], data[5], data[6], data[3] + "" + data[4], docFile, tempFolder, pdfFolder).
The Spreadsheet has the values from the columns "A" to "F". But in your arguments, data[6] is used. In this case, an error occurs at body.replaceText("{qty}", quantity).
From 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).
If your actual Spreadsheet is different from your sample Spreadsheet, the following modified script doesn't work. So, please be careful this. In this answer, I modified your script using your sample Spreadsheet.
In order to retrieve the image from the cell of =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.
The URL is retrieved from the formula and data you retrieved.
When above points are reflected to your script, it becomes as follows.
Modified script:
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;
}
Note:
In this modified script, your sample Spreadsheet is used. Please be careful this.
References:
fetch(url)
Related thread
Embed image uploaded by form to google doc using GAS
I am trying to write a script to pull information from a google sheet in a formatted google doc. I had the script running before the google update. It will no longer run. The error I usually get is attempted to execute, but could not save. Also, illegal character line 18. I can not figure out how to fix it. I am somewhat new to all this and teaching myself along the way. Help is appreciated. The script is below.
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('1-
TMh56SXOs6dWEsa1YWc5l9nbdTNn5pSIaCC23w-okc');
const destinationFolder =
DriveApp.getFolderById('1K6U9327cBby96eXoX-YPVTpWRgznt7I1')
const sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('TEST DOR
62');
const rows = sheet.getDataRange().getValues();
rows.forEach(function(row, index){
if (index === 0) return;
if (row[50]) return;
const copy = googleDocTemplate.makeCopy(`${row[2]}, ${row[0]}
Employee Details` , destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const friendlyDate = new Date(row[4]).toLocaleDateString();
body.replaceText('{{Q1}}', row[2]);
body.replaceText('{{Q2}}', row[3]);
body.replaceText('{{Q3}}', friendlyDate);
body.replaceText('{{Q4}}', row[5]);
body.replaceText('{{Q5}}', row[6]);
body.replaceText('{{Q6}}', row[7]);
body.replaceText('{{Q7}}', row[8]);
body.replaceText('{{Q8}}', row[9]);
body.replaceText('{{Q9}}', row[10]);
body.replaceText('{{Q10}}', row[11]);
body.replaceText('{{Q11}}', row[12]);
body.replaceText('{{Q12}}', row[13]);
body.replaceText('{{Q13}}', row[14]);
body.replaceText('{{Q14}}', row[15]);
body.replaceText('{{Q15}}', row[16]);
body.replaceText('{{Q16}}', row[17]);
body.replaceText('{{Q17}}', row[18]);
body.replaceText('{{Q18}}', row[19]);
body.replaceText('{{Q19}}', row[20]);
body.replaceText('{{Q20}}', row[21]);
body.replaceText('{{Q21}}', row[22]);
body.replaceText('{{Q22}}', row[23]);
body.replaceText('{{Q23}}', row[24]);
body.replaceText('{{Q24}}', row[25]);
body.replaceText('{{Q25}}', row[26]);
body.replaceText('{{Q26}}', row[27]);
body.replaceText('{{Q27}}', row[28]);
body.replaceText('{{Q28}}', row[29]);
body.replaceText('{{Q29}}', row[30]);
body.replaceText('{{Q30}}', row[31]);
body.replaceText('{{Q31}}', row[32]);
body.replaceText('{{Q32}}', row[33]);
body.replaceText('{{Q33}}', row[34]);
body.replaceText('{{Q34}}', row[35]);
body.replaceText('{{Q35}}', row[36]);
body.replaceText('{{Q36}}', row[37]);
body.replaceText('{{Q37}}', row[38]);
body.replaceText('{{Q38}}', row[39]);
body.replaceText('{{Q39}}', row[40]);
body.replaceText('{{Q40}}', row[41]);
body.replaceText('{{Q41}}', row[42]);
body.replaceText('{{Q42}}', row[43]);
body.replaceText('{{Q43}}', row[44]);
body.replaceText('{{Q44}}', row[45]);
doc.saveAndClose();
const url = doc.getUrl();
sheet.getRange(index + 1, 46).setValue(url)
})
}
I see at least two problems:
Bad formatting. The 'line 18' (the number depends on formatting as well) is broken. Probably it should be:
const copy = googleDocTemplate.makeCopy(`${row[2]}, ${row[0]} Employee Details`, destinationFolder);
And probably here:
doc.saveAndClose();
const url = doc.getUrl();
You close the doc and then you're trying to get its URL. It doesn't look right.
I'd propose to change the lines this way:
const url = doc.getUrl();
doc.saveAndClose();
Just in case, here is the code with fixed formatting. Try it:
function onOpen() {
const ui = SpreadsheetApp.getUi();
const menu = ui.createMenu('AutoFill Docs');
menu.addItem('Create New Docs', 'createNewGoogleDocs')
menu.addToUi();
}
function createNewGoogleDocs() {
const googleDocTemplate = DriveApp.getFileById('1-TMh56SXOs6dWEsa1YWc5l9nbdTNn5pSIaCC23w-okc');
const destinationFolder = DriveApp.getFolderById('1K6U9327cBby96eXoX-YPVTpWRgznt7I1')
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('TEST DOR 62');
const rows = sheet.getDataRange().getValues();
rows.forEach(function (row, index) {
if (index === 0) return;
if (row[50]) return;
const copy = googleDocTemplate.makeCopy(`${row[2]}, ${row[0]} Employee Details`, destinationFolder)
const doc = DocumentApp.openById(copy.getId())
const body = doc.getBody();
const friendlyDate = new Date(row[4]).toLocaleDateString();
body.replaceText('{{Q1}}', row[2]);
body.replaceText('{{Q2}}', row[3]);
body.replaceText('{{Q3}}', friendlyDate);
// the loop instead of the 40 lines of code
for (var i = 4; i < 45; i++) {
body.replaceText('{{Q'+ i +'}}', row[i+1]);
}
const url = doc.getUrl();
doc.saveAndClose();
sheet.getRange(index + 1, 46).setValue(url)
})
}
This is the script I'm trying to get working. I'm a newbie here, and so maybe someone could help me a little bit. As soon as the form is summited, on Google sheet are calculated two values based on the data which were collected on formsubmit(using arrayformula for the entire column). The values from those cells where array formula is applied, are not saved on my pdf. Only the values which were entered over the form are available on pdf.
any ideas?
Thank you all!
function afterFormSubmit(e) {
const info = e.namedValues;
const pdfFile = createPDF(info);
const entryRow = e.range.getRow();
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("students").getRange(entryRow, 9).setValue(pdfFile.getUrl());
}
function createPDF(info){
const pdfFolder = DriveApp.getFolderById("my pdf folder");
const tempFolder = DriveApp.getFolderById("my temp folder");
const templateDOC = DriveApp.getFileById("my template doc");
const newTempFile = templateDOC.makeCopy(tempFolder);
const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
body.replaceText("{_}", info['Marcaj de timp'][0]);
body.replaceText("{__}", info['First name'][0]);
body.replaceText("{___}", info['Last name'][0]);
body.replaceText("{____}", info['Total score'][0]);
body.replaceText("{_____}", info['Results'][0]);
openDoc.saveAndClose();
const blobPDF = newTempFile.getAs(MimeType.PDF);
const pdfFile = pdfFolder.createFile(blobPDF).setName(info['First name'][0] + " " + info['Last name'][0]);
return pdfFile;
}