I am unable to automate the population of footers in google docs - google-apps-script

I am trying to automate the population of the footer of a google doc that I have with the values that a pop up dialogue gathers from the users when they open the document. The code that I am using can be seen below, however it only works with the footer of the first page, when and only when I activate the option "Different first page". If I de-activate it (because I want the footer to be consistent within the whole document) nothing changes (not even the first page of the document) when I run the script.
The footer looks like this:
Blablbla / ##value1##
Title: “##value2##” - blablabla
The script to substitute value1 and value2 looks like this:
function myFunction() {
// Display a dialog box for each field you need information for.
var ui = DocumentApp.getUi();
var value1Response = ui.prompt('Enter the value1');
var value2Response = ui.prompt('Enter the value2');
var date = new Date();
//Make a copy of the template file
var documentId = DriveApp.getFileById('google document Id').makeCopy().getId();
//Rename the copied file
DriveApp.getFileById(documentId).setName(value2Response.getResponseText() + date);
//Get the document footer as a variable
var footer = DocumentApp.openById(documentId).getFooter();
footer.replaceText('##value1##', value1Response.getResponseText());
footer.replaceText('##value2##', value2Response.getResponseText());
}

This works for me.
function myFunction() {
var doc=DocumentApp.getActiveDocument();
var ui = DocumentApp.getUi();
var value1Response = ui.prompt('Enter the value1');
var value2Response = ui.prompt('Enter the value2');
var value3Response = ui.prompt('Enter FileName');
var date = new Date();
var documentId = doc.getId();
var footer = doc.getFooter();
footer.replaceText('##value1##', value1Response.getResponseText());
footer.replaceText('##value2##', value2Response.getResponseText());
var doc=DocumentApp.openById("Doc Id");
var footer=doc.getFooter();
doc.setName(value3Response.getResponseText());
footer.replaceText('##value1##', value1Response.getResponseText());
footer.replaceText('##value2##', value2Response.getResponseText());
}

Related

replaceText working in one instance but not in another

The script is triggered when a Google form is submitted and then auto-fills a Google doc.
It worked perfectly before I added var servicesPTY = e.values[117]; and replaced all the placeholders perfectly. But as soon as I add it then the executions indicator show completed but no documents are produced anymore. The document has placeholders that look like this: {{servicesPTY}} {{regNumberPTY}} {{tradingNamePTY}}
And the code looks like this:
function myFormSubmitPTY(e) {
var regNumberPTY = e.values[112];
var taxNumberPTY = e.values[111];
var tradingNamePTY = e.values[113];
var servicesPTY = e.values[117];
var file = DriveApp.getFileById("16OwyBIZAD2pwkuUXZnYSj-9WB6ObGGRXiEjDLa1tcjw");
var folder = DriveApp.getFolderById("1kogpJdxHLwuEhbVyh2oiIgTPH0SNac2m");
var copy = file.makeCopy(tradingNamePTY, folder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
if (type == "PTY (LTD)") {
body.replaceText("{{servicesPTY}}",servicesPTY);
body.replaceText("{{regNumberPTY}}", regNumberPTY);
body.replaceText("{{tradingNamePTY}}", tradingNamePTY);
doc.saveAndClose();
}
}
This works for me:
function testmyFormSubmit() {
var e={values:["one","two","three","four"]};
myFormSubmitPTY(e);
}
var type="PTY (LTD)";//global
function myFormSubmitPTY(e) {
var regNumberPTY = e.values[0];
var taxNumberPTY = e.values[1];
var tradingNamePTY = e.values[2];
var servicesPTY = e.values[3];
var file = DriveApp.getFileById("fileid");
var folder = DriveApp.getFolderById("folderid");
var copy = file.makeCopy(tradingNamePTY, folder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
if (type=="PTY (LTD)") {
body.replaceText("{{servicesPTY}}",servicesPTY);
body.replaceText("{{regNumberPTY}}", regNumberPTY);
body.replaceText("{{tradingNamePTY}}", tradingNamePTY);
doc.saveAndClose();
}
}
file name: three
pattern order:
{{servicesPTY}}
{{regNumberPTY}}
{{tradingNamePTY}}
output order:
four
one
three
I must have deselected the trigger for PTY. When I looked at the stackdriver logs I noticed PTY had none. Must have happened in the early hours of the morning. Thanks though. You guys put me on the right track

How can I replace the text in a Google Doc header with a script?

I am trying to create a letterhead template for coworkers. I have tried the following, but it does not work:
function myFunction() {
// Display dialog boxes
var ui = DocumentApp.getUi();
var nameResponse = ui.prompt('Enter your name');
var positionResponse = ui.prompt('Enter your position');
var phoneResponse = ui.prompt('Enter your phone number');
var docNameResponse = ui.prompt('Enter a name for your Google Doc');
//Make a copy of the template file
var documentId = DriveApp.getFileById('ID-goes-here').makeCopy().getId();
//Rename the copied file
DriveApp.getFileById(documentId).setName(docNameResponse.getResponseText());
//Get the document header as a variable
var header = DocumentApp.openById(documentId).getHeader();
//Insert the entries into the document
header.replaceText('##name##', nameResponse.getResponseText());
header.replaceText('##position##', positionResponse.getResponseText());
header.replaceText('##phone##', phoneResponse.getResponseText());
}
If I change the header variable to .getBody, I am able to replace the placeholder text (providing I copy it to the body section), but it does not work with getHeader.
For anybody getting this problem in the future.
getHeader() only works for me if I go to the header in google docs double click it. Then it will open the header section and then click on "Different first page". Only then it worked for me.
Don't ask why

Google Spreadsheet show URL to a new File in pop up window

I am currently trying to show some sort of pop up with a link to a new file that was created in the drive.
let me explain.
i have a database of employees
my spreadsheet as a button that copies information from a particular employee and then creates and fills another file with his info that then is saved/printed/or exported as pdf.
the script gets the URL of the file after this one is created, but i cant show it in the pop up window as a clickable link. here is my code.
(i am sorry if its a messy code, i learned 1 month ago)
function applicationcreator () {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var database = ss.getSheetByName("Database")
var rowname = database.getActiveCell().getRow()
var name = database.getRange(rowname, 4).getValues()
var ss2 = SpreadsheetApp.create("Resume")
var skill = ss2.getSheetByName("Sheet1")
var url =ss2.getUrl()
var form = ss.getSheetByName("Profilegenerator")
var copy = form.copyTo(ss2)
var form2 = ss2.getSheetByName("Copy of Profilegenerator").setName("Resume " + name)
var htmlOutput = HtmlService
.createHtmlOutput(url)
.setWidth(250) //optional
.setHeight(50); //optional
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Employee Profile');
}
I greatly apreciate your help.

Directly Update Doc Template from Form

Is it possible to submit a form and update a Doc template from the form contents? The only way I have seen for doing anything similar was submitting the form to a spreadsheet and having an onFormsubmit function within the spreadsheet update the template.
Is this not possible via forms and a container bound script?
Thanks
yes it's possible, but it's not easy (essentially using doc as template, there is no evident tools for that)
I have initially gotten this to work replacing a few lines in my document template, but now nothing is working for some reason. I did edit the text in the template at one point so I am not sure if that changes the file ID. It creates the copy, but the replaceText is not working. Code attached:
`function onFormSubmit(e) {
var dt = Utilities.formatDate(new Date(), 'GMT', "MM/dd/yyyy")
var submitter = Session.getActiveUser().getEmail();
var copyDoc = DriveApp.getFileById('1t5r8IxLgunJ17J2tXkHfw3LRfuXQeTlP5MZURC-23e0').makeCopy('tmpAddress').getId();
var newDoc = DocumentApp.openById(copyDoc);
var body = newDoc.getBody();
var itemResponse = e.response;
var name = itemResponse.getItemResponses()[0].getResponse();
var email = itemResponse.getItemResponses()[1].getResponse();
var pphone = itemResponse.getItemResponses()[2].getResponse();
var paddress = itemResponse.getItemResponses()[3].getResponse();
var palot = itemResponse.getItemResponses()[4].getResponse();
var pcity = itemResponse.getItemResponses()[5].getResponse();
var pstate = itemResponse.getItemResponses()[6].getResponse();
var pzip = itemResponse.getItemResponses()[7].getResponse();
var nphone = itemResponse.getItemResponses()[8].getResponse();
var naddress = itemResponse.getItemResponses()[9].getResponse();
var nalot = itemResponse.getItemResponses()[10].getResponse();
var ncity = itemResponse.getItemResponses()[11].getResponse();
var nstate = itemResponse.getItemResponses()[12].getResponse();
var nzip = itemResponse.getItemResponses()[13].getResponse();
body.replaceText('#{name}', name);
body.replaceText('#{email}', email);
body.replaceText('#{pphone}', pphone);
body.replaceText('#{paddress}', paddress);
body.replaceText('#{palot}', palot);
body.replaceText('#{pcity}', pcity);
body.replaceText('#{pstate}', pstate);
body.replaceText('#{pzip}', pzip);
body.replaceText('#{nphone}', nphone);
body.replaceText('#{naddress}', naddress);
body.replaceText('#{nalot}', nalot);
body.replaceText('#{ncity}', ncity);
body.replaceText('#{nstate}', nstate);
body.replaceText('#{nzip}', nzip);
body.replaceText('#{dt}', dt)
body.replaceText('#{submitter}',submitter)
newDoc.saveAndClose();
}
`

Google Form Document Automation

I have been receiving 2 main issues back upon a google form submission- the first being that the e.values I have set up cannot be read and the second that the Doclist is not defined. I have already set the triggers for the e.values and tried using e.namedvalues as well. I would like to eliminate the second issue by sending a doc and not a pdf to the form submitter, however it is most important that I get the first issue addressed first.
I am using framework that I have modified to fit my own documents, so that may be where I am encountering issues.
// G. Hopper
// Subcontract Generator
// Get template from Google00 Docs and name it
var docTemplate = "16bdl2myh7f0ywlxmf3iimq4Nz6-j8VTs7hkMbshRjp0";
var docName = "Subcontract";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var sub_name = e.namedValues["Subcontractor Name"].toString();
var sub_address = e.namedValues["Subcontractor address"].toString();
var proj_name = e.namedValues["Project Name"].toString();
var own_name = e.namedValues["Owner Name"].toString();
var own_address = e.namedValues["Owner Address"].toString();
var designer = e.namedValues["Design Firm"].toString();
var design_address = e.namedValues["Design Firm Address"].toString();
var proj_address = e.namedValues["Project Address"].toString();
var scope = e.namedValues["Scope of work"].toString();
var date_bid = e.namedValues["Date on bid"].toString();
var sub_value = e.namedValues["Subcontract Value"].toString();
var date_pay = e.namedValues["Date of the month pay applications are due"].toString();
var email = e.namedValues["Your email address"].toString();
var day = e.namedValues["Day"].toString();
var month_year = e.namedValues["Month and Year"].toString();
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy('Subcontract for '+sub_name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,in our google doc template
copyBody.replaceText('keySubName', sub_name);
copyBody.replaceText('keySubAddress', sub_address);
copyBody.replaceText('keyProject', proj_name);
copyBody.replaceText('keyOwner', own_name);
copyBody.replaceText('keyOwnAddress', own_address);
copyBody.replaceText('keyDesigner',designer);
copyBody.replaceText('keyDesignAddress', design_address);
copyBody.replaceText('keyProjAdd', proj_address);
copyBody.replaceText('keyScope', scope);
copyBody.replaceText('keyBidDate', date_bid);
copyBody.replaceText('KeySubValue', sub_value);
copyBody.replaceText('keyPayApp', date_pay);
copyBody.replaceText('keyDay', day);
copyBody.replaceText('keyMonthDay', month_year);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = 'Subcontract'
var body = "Here is your subcontract for " + sub_name + "";
MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}