How to add cc in Google Apps Script and onFormSubmit trigger - google-apps-script

I am not sure how can I add a line for cc. Below is the script i have. Grateful if anyone can help. Thanks!
Meanwhile, I know there is a trigger called onFormSubmit trigger. May I know how can I send an email according to the specific answer coming in from a form?
For instance, in the Google form. I have question like Which colour do you like? Red or Yellow. If someone picked yellow, an email would be sent. However, I am not sure how to do that.
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Send Email",
functionName : "sendEmails"
}
];
sheet.addMenu("Let's do it", entries);
};
function hindex(header_row, value){
for (var j = 0; j < header_row.length; j++) {
if(value == header_row[j]) return j;
}
return 0;
}
function sendEmails() {
var htmlcode = String(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("HTML").getRange(1, 1).getValue());
var replyTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sender").getRange(2, 2).getValue();
var name_shown = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sender").getRange(1, 2).getValue();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Receiver List");
var htmlsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Testhtml");
var startRow = 2; // First row of data to process
var LastRow = sheet.getLastRow(); // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, LastRow-1, 50);
var data = dataRange.getValues();
for (var i = 1; i < data.length; i++) {
var row = data[i];
var name = row[0];
var emailAddress = row[1];
var subject = row[2];
var confirm = row[3];
var EmailSent = row[4];
var strings = data[0];
var obj = {};
var message = htmlcode;
for (var k = 0; k < strings.length; k++){
if(strings[k].length == 0)continue;
obj[strings[k]] = row[hindex(data[0],strings[k])];
var reg = new RegExp("\{\{"+strings[k]+"\}\}", "g");
//htmlsheet.getRange(k+1, 3).setValue(reg);
message = message.replace(reg,obj[strings[k]])
}
/*
var r1 = row[hindex(data[0],"r1")];
var r2 = row[hindex(data[0],"r2")];
var r3 = row[hindex(data[0],"r3")];
var r4 = row[hindex(data[0],"r4")];
var r5 = row[hindex(data[0],"r5")];
var r6 = row[hindex(data[0],"r6")];
var r7 = row[hindex(data[0],"r7")];
var r8 = row[hindex(data[0],"r8")];
var r9 = row[hindex(data[0],"r9")];
var r10 = row[hindex(data[0],"r10")];
var r11 = row[hindex(data[0],"r11")];
var r12 = row[hindex(data[0],"r12")];
var r13 = row[hindex(data[0],"r13")];
var q1 = row[hindex(data[0],"q1")];
var r14 = row[hindex(data[0],"r14")];
var q2 = row[hindex(data[0],"q2")];
var q3 = row[hindex(data[0],"q3")];
var q4 = row[hindex(data[0],"q4")];
var q5 = row[hindex(data[0],"q5")];
var r15 = row[hindex(data[0],"r15")];
var q6 = row[hindex(data[0],"q6")];
var r16 = row[hindex(data[0],"r16")];
var q7 = row[hindex(data[0],"q7")];
message = htmlcode.replace(/\{\{r1\}\}/,r1)
.replace(/\{\{r2\}\}/,r2)
.replace(/\{\{r3\}\}/,r3)
.replace(/\{\{r4\}\}/,r4)
.replace(/\{\{r5\}\}/,r5)
.replace(/\{\{r6\}\}/,r6)
.replace(/\{\{r7\}\}/,r7)
.replace(/\{\{r8\}\}/,r8)
.replace(/\{\{r9\}\}/,r9)
.replace(/\{\{r10\}\}/,r10)
.replace(/\{\{r11\}\}/,r11)
.replace(/\{\{r12\}\}/,r12)
.replace(/\{\{r13\}\}/,r13)
.replace(/\{\{r14\}\}/,r14)
.replace(/\{\{r15\}\}/,r15)
.replace(/\{\{r16\}\}/,r16)
.replace(/\{\{q1\}\}/,q1)
.replace(/\{\{q2\}\}/,q2)
.replace(/\{\{q3\}\}/,q3)
.replace(/\{\{q4\}\}/,q4)
.replace(/\{\{q5\}\}/,q5)
.replace(/\{\{q6\}\}/,q6)
.replace(/\{\{q7\}\}/,q7)
.replace(/\{\{name\}\}/,name);
*/
htmlsheet.getRange(1, 1).setValue(message);
htmlsheet.getRange(1, 2).setValue(JSON.stringify(obj));
if (confirm == "Yes" && EmailSent != "Email Sent") {
MailApp.sendEmail(emailAddress,subject, "", {
htmlBody: message,
replyTo: replyTo,
name: name_shown
});
sheet.getRange(startRow + i, 5).setValue("Email Sent");
}
}
}

Add cc:
The cc is an advanced parameter at sendEmail(recipient, subject, body, options), like htmlBody or replyTo. Therefore, when using sendEmail, you should add it this way:
MailApp.sendEmail(emailAddress,subject, "", {
htmlBody: message,
replyTo: replyTo,
name: name_shown,
cc: "YOUR_DESIRED_CC_EMAIL"
});
Use onFormSubmit:
There are two types of onFormSubmit triggers, one for the spreadsheet linked to the Form and the other for the Form itself. Depending on which one you install (see Installable Triggers for more information on how to install a trigger), you'd retrieve the value of a response in different ways, using the corresponding event object.
It's probably easier to get this information via the Sheets event object, so I'd suggest you to use the Sheets trigger. In this case, the event object has a field namedValues, which refers to an object containing the question names and values from the form submission. In the function triggered by the form submission, just find the key that corresponds to the item you want to fetch the response from, and retrieve its value. For example:
function triggeredByFormSubmit(e) {
const submittedColor = e.namedValues["Which colour do you like?"];
// ...
}
If you installed a Form's onFormSubmit instead, you should find the item response via the event property response, which corresponds to the FormResponse.
Note: Please note that this is supposed to run automatically when a user submits a form. If you try to execute it directly from the editor, or from the custom menu, it will fail because the event object (in the sample above, e) won't be defined.

Related

How to allow non-sheet owners run scripts that involve protected cells

I have the script below where some cells are protected because they contain formula but I can script linked to buttons that when executed, it updates the cell values in these protected cells, this is fine if you are the sheet owner but if you are not you get a error saying 'You are editing protected cells....'
I have seen some solutions where the script has been deployed as a web app and then set so it always runs as the owner but can't get this working for my use case, I deployed and set as to always run as me but this only seems like half the solution?
My code is below:
//
// Save Data
function submitData() {
var SPREADSHEET_NAME = "Data";
var SEARCH_COL_IDX = 0;
var RETURN_COL_IDX = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
var str = formSS.getRange("A10").getValue();
var values = ss.getSheetByName(SPREADSHEET_NAME).getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] != str ) {
//SpreadsheetApp.getUi().alert(' "Dmp #' + formSS.getRange("A4").getValue() + ' "');
// return row[RETURN_COL_IDX];
//} else {
//Input Values
var values1 = [[formSS.getRange("A10").getValue(),
formSS.getRange("B10").getValue(),
formSS.getRange("C10").getValue(),
formSS.getRange("D10").getValue(),
formSS.getRange("E10").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("G10").getValue(),
formSS.getRange("H10").getValue(),
formSS.getRange("I10").getValue(),
formSS.getRange("J10").getValue(),
formSS.getRange("K10").getValue()]];
var values2 = [[formSS.getRange("A10").getValue(),
formSS.getRange("B10").getValue(),
formSS.getRange("C10").getValue(),
formSS.getRange("D10").getValue(),
formSS.getRange("E10").getValue(),
formSS.getRange("F10").getValue(),
formSS.getRange("G10").getValue(),
formSS.getRange("I10").getValue(),
formSS.getRange("J10").getValue(),
formSS.getRange("K10").getValue()]];
values2[0].forEach(function(val) {
if (val === "") {
throw new Error("Please fill in Project, Category, Subsystem, Description and Created By Fields.");
}
})
// Save New Data
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 11).setValues(values1);
SpreadsheetApp.getUi().alert(' New Record Created ');
formSS.getRange("D10").clearContent();
formSS.getRange("E10").clearContent();
formSS.getRange("F10").clearContent();
formSS.getRange("G10").clearContent();
formSS.getRange("H10").clearContent();
formSS.getRange("I10").clearContent();
formSS.getRange("J10").setValue(new Date())
return row[RETURN_COL_IDX];
}
}
}
//=========================================================
// Clear form
function clearCell() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
formSS.getRange("D10").clearContent();
formSS.getRange("E10").clearContent();
formSS.getRange("F10").clearContent();
formSS.getRange("G10").clearContent();
formSS.getRange("I10").clearContent();
formSS.getRange("J10").setValue(new Date())
return true ;
}
//=====================================================================
var SPREADSHEET_NAME = "Data";
var SEARCH_COL_IDX = 0;
var RETURN_COL_IDX = 0;
function searchStr() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
var str = formSS.getRange("F4").getValue();
var values = ss.getSheetByName(SPREADSHEET_NAME).getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
formSS.getRange("A6").setValue(row[0]) ;
formSS.getRange("B6").setValue(row[1]);
formSS.getRange("C6").setValue(row[2]);
formSS.getRange("D6").setValue(row[3]);
formSS.getRange("E6").setValue(row[4]);
formSS.getRange("F6").setValue(row[5]);
formSS.getRange("G6").setValue(row[6]);
formSS.getRange("H6").setValue(row[7]);
formSS.getRange("I6").setValue(row[8]);
formSS.getRange("J6").setValue(row[9]);
return row[RETURN_COL_IDX];
}
}
}
//===================================================================
function rowDelete() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
var ui = SpreadsheetApp.getUi();
var response = ui.alert(
'Are you sure you want to delete this record?',
ui.ButtonSet.YES_NO);
// Process the user's response.
if (response == ui.Button.YES) {
var str = formSS.getRange("F4").getValue();
var values = ss.getSheetByName(SPREADSHEET_NAME).getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
var INT_R = i+1
datasheet.deleteRow(INT_R) ;
formSS.getRange("A6").clearContent();
formSS.getRange("B6").clearContent();
formSS.getRange("C6").clearContent();
formSS.getRange("D6").clearContent();
formSS.getRange("E6").clearContent();
formSS.getRange("F6").clearContent();
formSS.getRange("G6").clearContent();
formSS.getRange("H6").clearContent();
formSS.getRange("I6").clearContent();
formSS.getRange("J6").clearContent();
return row[RETURN_COL_IDX];
}
}
}
}
//====================================================================
function updateData() {
var SPREADSHEET_NAME = "Data";
var SEARCH_COL_IDX = 0;
var RETURN_COL_IDX = 0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Tool"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
var str = formSS.getRange("A6").getValue();
var values = ss.getSheetByName(SPREADSHEET_NAME).getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
var INT_R = i+1
formSS.getRange("J6").setValue(new Date())
var values1 = [[formSS.getRange("A6").getValue(),
formSS.getRange("B6").getValue(),
formSS.getRange("C6").getValue(),
formSS.getRange("D6").getValue(),
formSS.getRange("E6").getValue(),
formSS.getRange("F6").getValue(),
formSS.getRange("G6").getValue(),
formSS.getRange("H6").getValue(),
formSS.getRange("I6").getValue(),
formSS.getRange("J6").getValue()]];
var values2 = [[formSS.getRange("A6").getValue(),
formSS.getRange("B6").getValue(),
formSS.getRange("C6").getValue(),
formSS.getRange("D6").getValue(),
formSS.getRange("E6").getValue(),
formSS.getRange("F6").getValue(),
formSS.getRange("G6").getValue(),
formSS.getRange("I6").getValue(),
formSS.getRange("J6").getValue()]];
values2[0].forEach(function(val) {
if (val === "") {
throw new Error("Please fill in Revisions, Project, Category, Subsystem, Description and Updated By Fields.");
}
})
datasheet.getRange(INT_R, 1, 1, 10).setValues(values1);
formSS.getRange("A6").clearContent();
formSS.getRange("B6").clearContent();
formSS.getRange("C6").clearContent();
formSS.getRange("D6").clearContent();
formSS.getRange("E6").clearContent();
formSS.getRange("F6").clearContent();
formSS.getRange("G6").clearContent();
formSS.getRange("H6").clearContent();
formSS.getRange("I6").clearContent();
formSS.getRange("J6").clearContent();
formSS.getRange("E4").clearContent();
SpreadsheetApp.getUi().alert(' Record Updated ');
return row[RETURN_COL_IDX];
}
}
}
There are several posts about this, I'll paste a response from one from yesterday. What I recommend specifically in your case is to run the script when there's an edit bye the user in a certain cell. For example a Tickbox, or a Drop-down menu (in a cell) that allows the user to select which function to run:
If you already have an onEdit function working, that's a simple trigger run by whoever is editing the sheet. Meaning that if you protect column A, it won't be editable by that simple trigger because the user won't have permissions
In order to work this out, I encourage you to protect your column as explained here, change your name function or extract in a new function the part about this specific code you're talking about; and set an installable trigger that runs on event. This way it'll be run as you used to but as it came from your own account. As you have permissions for editing ColA the timestamp will be set by the installable trigger but the other user won't be able to edit it since he/she doesn't have the permissions. Try it and let me know!

how to use GmailApp.Sendemail when certain conditions are met?

I have 5 columns (C2:C, E2:E, F2:F, J2:J, L2:L) in a sheet that i'd like to use when certain conditions are met to send an email
if F2:G <= E2:E, send email with subject like "check out C2:C"
if E2:E >= L2:L AND J2:J <> "", send email with subject like "take a look at C2:C"
I did a bit of searching and found 2 different instances to check for a condition and to send email but not able to put the 2 together to make it work like the above. Below are 2 different scripts, one is commented out just to show what i was working with...
Here is what I have so far:
function Alert() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Display");
var ERange = sheet.getRange("E2:E").getValues();
var FRange = sheet.getRange("F2:F").getValues();
var results = [];
for(var i=0;i>ERange.length;i++){
if(ERange[i]<=FRange[i]){
results.push("this line met conditions3 "+(i+2)); // +2 because the loop start at zero and first line is the second one (E2)
}
}
MailApp.sendEmail('xx#yy.com', 'subject', results.join("\n"));
};
//function AlertEmails() {
// var ss = SpreadsheetApp.getActiveSpreadsheet();
// ss.setActiveSheet(ss.getSheetByName("EmailDetails"));
// var sheet = SpreadsheetApp.getActiveSheet();
// var dataRange = sheet.getRange("A2:C3");
// var data = dataRange.getValues();
// for (i in data) {
// var rowData = data[i];
// var emailAddress = rowData[1];
// var recipient = rowData[0];
// var message1 = rowData[2];
// var message = 'Dear ' + recipient + ',\n\n' + message1;
// var subject = 'googleSheets test automated script!';
// GmailApp.sendEmail(emailAddress, subject, message);
// }
//}
thanks guys!!
I would write it like this:
function Alert() {
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName("Display");
var Evalues=sheet.getRange(2,5,sheet.getLastRow()-1,1).getValues();
This range format is a little less error prone. With E2:E you often get nulls between the bottom of data and maxrows.
var Fvalues=sheet.getRange(2,6,sheet.getLastRow()-1,1).getValues();
var results=[];
In the following line you have i>Evalues.length which is not true so it
terminated immediately.
for(var i=0;i<Evalues.length;i++){
You and I both left out the column indices. I just added them into the line below:
if(Evalues[i][0]<=Fvalues[i][0]){
results.push("This line met conditions3 "+(i+2));
}
}
MailApp.sendEmail('xx#yy.com', 'subject',results.join("\n"));
}

MailApp.SendEmail script for multiple tabs

I have been looking how to write the script to check and run on multiple tabs. What I have now, I would need to copy/paste and set execution triggers one by one.
Script I have:
function SendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("1");
var value = sheet.getRange("F3").getValue();
var subject = "subject";
var message = "message";
if (value >0){
MailApp.sendEmail("email", subject, message);
}
}
I have this running every 8 hours (trigger run).
I am looking to add more tabs so that for each tab the script can check and send an email if the value of individual tabs is more than 0.
Any ideas on how to do this?
The value in every tab is under F3. Tab names, for example, 1,2,3,4...
Thank you!!!
This should do what you looking for if I understood the issue correctly.
function SendEmail() {
//Get the Spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Get all Sheets in the Spreadsheet
var sheets = ss.getSheets();
//Create a variable for the email address, subject and message
var email = "";
var subject = "";
var message = "";
//Can change the starting sheet by changing the value of "i" - 0 will be the first sheet.
for (var i = 0; i < sheets.length; i++){
var sheet = sheets[i];
var value = sheet.getRange("F3").getValue();
switch(sheet.getSheetName) {
case "Sheet1":
email = "email1#domain.com";
break;
case "Sheet2":
email = "email2#domain.com";
break;
default:
value = 1
email = "yourEmailAddress#domain.com";
subject = "Message Failed to Send";
message = "There was an error sending email, please investigate";
}
if (value >0){
MailApp.sendEmail(email, subject, message);
}//END OF IF STATEMENT
}//END OF FOR LOOP
}//END OF FUNCTION

Google Apps Script - How to get email addresses from Sheet2 and send email

I have a Spreadsheet that stores Task on on Sheet 1. When a task is completed a email is sent. Sheet 2 holds the specific email addresses for send to, cc, and reply to. I am able to loop through sheet 2 and get the email addresses for each column. I want to be able to get those email address into my options for the send email in MailApp. I am unable to get the email addresses out of the loop for sheet 2. See code below. Any help would be appreciate.
var EMAIL_SENT = "EMAIL_SENT";
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheets()[0];
var sheet2 = ss.getSheets()[1];
var startRow = 2;
var lastRow1 = sheet1.getLastRow()-1;
var lastCol1 = sheet1.getLastColumn();
var lastRow2 = sheet2.getLastRow()-1;
var lastCol2 = sheet2.getLastColumn();
var sh1Range = sheet1.getRange(startRow, 1, lastRow1, lastCol1).getValues();
var sh2Range = sheet2.getRange(startRow, 1, lastRow2, lastCol2).getValues();
var subject = "Test Email";
//sheet2 Email Loop
for (var i = 0; i < sh2Range.length; ++i){
var emails = sh2Range[i];
var to = emails[0];
var cc = emails[1];
var replyTo = emails[2];
}
//sheet1 Data Loop and Send Email
for (var j = 0; j < sh1Range.length; ++j){
var data = sh1Range[j];
var pName = data[0];
var pID = data[1];
var pm = data[2];
var dd = Utilities.formatDate(new Date(data[3]), "America/New_York", "MMMM dd, yyyy");
var team1 = data[4];
var status = data[7];
if (team1 == "Task Completed" && status !== EMAIL_SENT){
var htmlBody;
htmlBody = "Project Name: "+pName+"<br>"+"Project ID: "+pID+"<br>"+"Project Manager: "+pm+"<br>"+"Due Date: "+dd+"<br>";
var optAdvancedArgs = {replyTo: replyTo, cc: cc, name: "Test Email", htmlBody: htmlBody};
//Logger.log(htmlBody);
//Logger.log(optAdvancedArgs);
}
}
}
The question is a little broad, and there are many ways to do what you're looking for. It's tough to say what you should choose when the specifics of your application aren't clear, such as how many addresses are going to be on Sheet 2, and if you really need to go through an 'if' statement, but I'll offer two potential solutions here that may be of use.
Solution 1: What I was trying to ascertain in my comments above was if the email addresses list would be the same length as the list of projects (For example, if there are 10 projects on sheet 1, does that mean there are 10 rows of email addresses in sheet 2). It's still not clear to me, as you have a list of email addresses in your 'CC' address, yet only one in your 'Send to' and 'Reply to' addresses (so you don't really need to loop through the whole range, but perhaps that's required for some other part of the project). However, if that IS the case, then you can actually do all of this in one 'for' loop:
//sheet1 Data Loop and Send Email
for (var j = 0; j < sh1Range.length; ++j){
var data = sh1Range[j];
var pName = data[0];
var pID = data[1];
var pm = data[2];
var dd = Utilities.formatDate(new Date(data[3]), "America/New_York", "MMMM dd, yyyy");
var team1 = data[4];
var status = data[7];
if (team1 == "Task Completed" && status !== EMAIL_SENT){
var emails = sh2Range[i];
var to = emails[0];
var cc = emails[1];
var replyTo = emails[2];
var htmlBody;
htmlBody = "Project Name: "+pName+"<br>"+"Project ID: "+pID+"<br>"+"Project Manager: "+pm+"<br>"+"Due Date: "+dd+"<br>";
var optAdvancedArgs = {replyTo: replyTo, cc: cc, name: "Test Email", htmlBody: htmlBody};
//Logger.log(htmlBody);
//Logger.log(optAdvancedArgs);
}
}
Solution 2: Otherwise, you could place the email addresses into an array when you loop through them:
var addresses = [];
for (var i = 0; i < sh2Range.length; ++i){
var emails = sh2Range[i];
var to = emails[0];
var cc = emails[1];
var replyTo = emails[2];
addresses.push([to,cc,replyTo])
}
Which will then allow you to access them using the bracket notation 'addresses[0][0]' later in the script:
var optAdvancedArgs = {replyTo: addresses[0][2], cc: addresses[j][1], name: "Test Email", htmlBody: htmlBody};
Both these solutions make a lot of presumptions on what you're doing, but they might guide you to either the answer you're looking for, or possible give you a more concrete idea of what kind of question you're asking.

How can I keep the leading zero's in my pdf generated documents from google spreadsheet?

I have created a google test form and test form response. In the response spreadsheet, I have written below script to capture the inputted values from the from > generate a pdf/doc > email the pdf & > save a copy of the doc in a target folder. The issue I'm running into is that the section labeled as passcode could start with a "0" or multiple "0's", which I have formatted in the script for the spreadsheet. However when it generates the PDF/DOC, the zero's are removed. Is there any way to keep the leading 0's when converted in the PDF/DOC?
function formatNumber(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("D2:D");
// Always show 4 digits
cell.setNumberFormat("0000");
}
var docTemplate = "11laarf0ThJ4mANX4KzHZCblVwRgphlf-bCblaUMl4oc";
var docName = "Test Form";
function onFormSubmit(e) {
var email_address = "jimmy111#gmail.com";
var Time_Stamp = e.namedValues["Timestamp"];
var full_name = e.namedValues["Name"];
var phone = e.namedValues["Phone Number"];
var passcode = e.namedValues["Passcode"];
var price = e.namedValues["Price"];
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(full_name+' '+docName)
.getId();
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getActiveSection();
copyBody.replaceText('keyDate', Time_Stamp);
copyBody.replaceText('keyName', full_name);
copyBody.replaceText('keyPhone', phone);
copyBody.replaceText('keyCode', passcode);
copyBody.replaceText('keyPrice', price);
copyDoc.saveAndClose();
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
var subject = "CIR Testform for "+ full_name + "";
var body = "Here is the registration form for "+ full_name +"";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
var targetFolder = DocsList.getFolderById('0B-LfisIjjXtvLW82X3o1UzNyY1U');
var file = DocsList.getFileById(copyId);
file.addToFolder(targetFolder);
file.removeFromFolder(DocsList.getRootFolder());
}
function assignEditUrls() {
var form = FormApp.openById('16zVePLm61yRsSZMaoDI5MWIeB-vGHZGEoR9J7uJ0CP8');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
var data = sheet.getDataRange().getValues();
var urlCol = 6; // column number where URL's should be populated; A = 1, B = 2 etc
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(responses[i].getEditResponseUrl());
}
for (var j = 1; j < data.length; j++) {
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j] [0].setMilliseconds(0))]:'']);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}
I've got a workaround to this issue, it's less than ideal but it's working.
Using the Pre-Filled form feature, enter a period in each response that requires the leading zero.
So if your user had entered "0123456", the recorded response will appear as "0.123456" you can then use substring to remove the first two charecters.
e.g.
var phone = e.namedValues["Phone Number"].substring(2);
This will then give you the leading zero. The only issue is accidental deletion of the period from the pre-filled form, although you can set a validation which checks for it, and then an error asking the user to enter it if they've removed it.
Not pretty, but we're up and running with that now at least.