0
votes

I am trying to use google app maker to enter data. I want to export that data to a google docs template and create a PDF.

I have tried this link

Document Merge with Google App Maker But I get an error

Creating new record: (Error) : Invalid argument: replacement at 
models.Clients.onAfterCreateEvent:13
at NewPage.Form1.Form1Header.Form1CreateButton.onClick:2:19

Wed Apr 03 10:27:12 GMT-500 2019
Creating new record failed.


Wed Apr 03 10:27:12 GMT-500 2019
(Error) : Invalid argument: replacement'

''' There is another form post link Google App Maker - Creating a contract. Methods to use? So I know its possible but I am new to making scripts and can't figure it out.

I have tried multiple different scripts Found here on Stackoverflow.

I want to be able to enter Data in App from APP maker. Than export to Google doc template. I am new to App Script and any help would be greatly appreciated Thank you

'''

var templateId = 'your template ID';
var filename = 'Document for Customer ' + record.ClientName + new Date();

var copyFile = DriveApp.getFileById(templateId).makeCopy(filename);

var copyDoc = DocumentApp.openById(copyFile.getId());
var copyBody = copyDoc.getBody();
var fields = app.metadata.models.Clients.fields;
for (var i in fields) {
var text = '<<' + fields[i].name + '>>';
var data = record[fields[i].name];
copyBody.replaceText(text, data);
}

'''

I am trying to get App maker data to import into a google doc template and than create a PDF of that doc with the imputed values in place of the placeholder.

I got it to link to the Google Docs but it comes back with a value of undefined. I am using SQL database

var templateId = '1IbbrRJhYSnfEmgUD1GYrjxEHeyseGtQFx-G3CzEC4mI';
var filename = 'Document for Customer ' + record.ClientName + new Date();

var copyFile = DriveApp.getFileById(templateId).makeCopy(filename);

var copyDoc = DocumentApp.openById(copyFile.getId());
var copyBody = copyDoc.getBody();
var fields = app.metadata.models.Clients.fields;

for (var i in fields) {
var text = '<<' + fields[i].name.valueOf() + '>>';
var data = record[fields[i].item];
copyBody.replaceText(text, data);
}

copyDoc.saveAndClose();
2

2 Answers

1
votes

I will try to be as specific as possible. I am new on scripting and App Maker as well and will share my experience with creating pdf files and working with App Maker. This is my first answer and I hope it works for you as it worked for me.

I know that you don’t want to use a template. In case you just want to create a PDF file with information retrieved from the application itself I would recommend to check the App Maker sample https://developers.google.com/appmaker/samples/email-pdf/. This sample contains a form and after you click on the button this takes the data entered on the app and send an email with the pdf generated from the application.

Now, for personal experience I prefer to use a template since you can customize the template and could send a personalized pdf file, however, this will depend on the purpose of why are you generating such pdf file. So here is my response based on my experience.

I used a Google Sheet template, to be able to modify such template I had to use the Spreadsheet Service https://developers.google.com/apps-script/reference/spreadsheet/. You will learn how to access to a Google Sheet and how to modify each cell from it, also how to modify specific sheets within the document.

I found https://gist.github.com/primaryobjects/6370689c6f5fd3799ea53f89551eced7 and gave me an idea how to create the pdf, this explains how to generate the pdf and store the pdf file into Google Drive. Also you have the idea how to send it through email using the App Maker sample above. This project is for a Google Sheet document, it creates an additional menu for you to export the current Google Sheet, you can use the Drive API to download the pdf if needed https://developers.google.com/drive/api/v3/manage-downloads.

I hope all this information was helpful to resolve your inquiry, I will try to find the app since I don’t have handy all the code and I have to check it again in order to provide samples here but hopefully this helps to find a way to achieve what you are looking for. I will search my code and will provide some examples as soon as I have more time to dive into my code since the app was created some time ago.

0
votes

I must have had some Binding incorrect cause after starting from scratch

var templateId = 'your template ID';
var filename = 'Document for Customer ' + record.ClientName + new Date();

var copyFile = DriveApp.getFileById(templateId).makeCopy(filename);

var copyDoc = DocumentApp.openById(copyFile.getId());
var copyBody = copyDoc.getBody();
var fields = app.metadata.models.Clients.fields;

for (var i in fields) {
var text = '<<' + fields[i].name + '>>';
var data = record[fields[i].name];
copyBody.replaceText(text, data);
}

copyDoc.saveAndClose();

from linked https://developers.google.com/apps-script/reference/spreadsheet/ worked sroperly