Using Google Apps Script, I have the following code that automatically sorts a table whenever I edit information in the table.
function onEdit(event){
var sheet = event.source.getActiveSheet();
var columnToSortBy = 3;
var tableRange = "A3:AQ11";
var range = sheet.getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: false } );
}
However, my entire table is populated by formulas that reference different sheets, and if I change a value in a different sheet, the tables values on the first sheet change, but it does not get sorted b/c the event did not occur on that sheet.
Is there a way to add functionality to sort the table whenever information in the table changes, rather than a manual edit to the table? I have looked into the onChange() event, but not sure how exactly it could be used.
onChange docs has an example:
From : https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("myFunction")
.forSpreadsheet(sheet)
.onChange()
.create();
how to add triggers: From http://www.labnol.org/internet/google-docs-email-form/20884/
/* Send Google Form Data by Email v3.0 */
/* Source: http://labnol.org/?p=20884 */
/**
* #OnlyCurrentDoc
*/
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers)
ScriptApp.deleteTrigger(triggers[i]);
ScriptApp.newTrigger("SendGoogleForm")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit().create();
}
function SendGoogleForm(e) {
if (MailApp.getRemainingDailyQuota() < 1) return;
// You may replace this with another email address
var email = "hello#ctrlq.org";
// Enter your subject for Google Form email notifications
var subject = "Google Form Submitted";
var s = SpreadsheetApp.getActiveSheet();
var columns = s.getRange(1, 1, 1, s.getLastColumn()).getValues()[0];
var message = "";
// Only include form fields that are not blank
for (var keys in columns) {
var key = columns[keys];
if (e.namedValues[key] && (e.namedValues[key] !== "")) {
message += key + ' :: ' + e.namedValues[key] + "\n\n";
}
}
MailApp.sendEmail(email, subject, message);
}
/* For support, contact the develop at www.ctrlq.org */
Go to the Run menu and choose Initialize. The Google Script will now require you to authorize the script – just click the Accept button and you’re done.
Or you can use an onOpen function to auto add, I think "FormEmailer" includes and example of how.
Related
I am creating a web app using Google Apps Script. So this app basically is meant to send quotations to user by collecting various parameters. The deployment has 3 views. The first asks for Email and Plant Type. The second view asks for 4 parameters. The third view shows recommendation after some processing on server side along with other 2 inputs.
The problem is that I have to append a row after the submit button is clicked on the third view. So that a new row is created in my linked google sheets as a database with all the inputs from user. The script was running fine when there was 1 view. But as I made 3 views, it's now not appending a new row in google sheets.
The Object "Info" is where I am storing inputs from user.
Here's the script:
<script>
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems);
});
var Info = {};
document.getElementById("btn1").addEventListener("click",dothis);
function dothis(){
Info.email = document.getElementById("email").value;
Info.ptype = document.getElementById("ptype").value;
}
document.getElementById("btn2").addEventListener("click",thenthis);
function thenthis(){
Info.quantity = document.getElementById("quantity").value;
Info.hotwt = document.getElementById("hotwt").value;
Info.coldwt = document.getElementById("coldwt").value;
Info.wetbt = document.getElementById("wetbt").value;
}
document.getElementById("submit").addEventListener("click",andthenthis);
function andthenthis(){
Info.recomcell = document.getElementById("recomcell").value;
Info.prefercells = document.getElementById("prefercells").value;
Info.coc = document.getElementById("coc").value;
google.script.run.UserClicked(Info);
document.getElementById("recomcell").value = "";
M.updateTextFields();
var mycoc = document.getElementById("coc");
mycoc.selectedIndex = 0;
M.FormSelect.init(mycoc);
var myprefercells = document.getElementById("prefercells");
myprefercells.selectedIndex = 0;
M.FormSelect.init(myprefercells);
}
</script>
Here's the other part of the code:
function UserClicked(Info){
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Database");
ws.appendRow([Info.email,Info.ptype,Info.quantity,Info.hotwt,Info.coldwt,Info.wetbt,Info.recomcell,Info.prefercells,Info.coc,new Date()]);
}
I want a notification/email when a cell value in column 5 of the spreasheet changes to 'Buy' which is based on formula =IF(AND(B2>D2),"Buy","Skip"). Attachment Link provided below. The Sheet autorefreshes based on Googlefinance data.
I have tried the below script, but it triggers notification/Email only when I change the cell manually. I am not a code writer, but have managed to find below script and added some tweaks of my own.
Can somebody please help me in resolving this or provide an alternate solution?
Also script written on third Party apps will be appreciated.
function triggerOnEdit(e)
{
showMessageOnUpdate(e);
}
function showMessageOnUpdate(e)
{
var range = e.range;
SpreadsheetApp.getUi().alert("range updated " + range.getA1Notation());
}
function checkStatusIsBuy(e)
{
var range = e.range;
if(range.getColumn() <= 5 &&
range.getLastColumn() >=5 )
{
var edited_row = range.getRow();
var strongBuy = SpreadsheetApp.getActiveSheet().getRange(edited_row,5).getValue();
if(strongBuy == 'Buy')
{
return edited_row;
}
}
return 0;
}
function triggerOnEdit(e)
{
showMessageOnBuy(e);
}
function showMessageOnBuy(e)
{
var edited_row = checkStatusIsBuy(e);
if(edited_row > 0)
{
SpreadsheetApp.getUi().alert("Row # "+edited_row+"Buy!");
}
}
function sendEmailOnBuy(e)
{
var buy_row = checkStatusIsBuy(e);
if(buy_row <= 0)
{
return;
}
sendEmailByRow(buy_row);
}
function sendEmailByRow(row)
{
var values = SpreadsheetApp.getActiveSheet().getRange(row,1,row,5).getValues();
var row_values = values[0];
var mail = composeBuyEmail(row_values);
SpreadsheetApp.getUi().alert(" subject is "+mail.subject+"\n message "+mail.message);
}
function composeBuyEmail(row_values)
{
var stock_name = row_values[0];
var cmp = row_values[1];
var volume = row_values[2];
var message = "The Status has changed: "+stock_name+" "+cmp+
" Volume "+volume;
var subject = "Strong Buy "+stock_name+" "+cmp
return({message:message,subject:subject});
}
function triggerOnEdit(e)
{
sendEmailOnBuy(e);
}
var admin_email='sendemail#gmail.com';
function sendEmailByRow(row)
{
var values = SpreadsheetApp.getActiveSheet().getRange(row,1,row,5).getValues();
var row_values = values[0];
var mail = composeBuyEmail(row_values);
SpreadsheetApp.getUi().alert(" subject is "+mail.subject+"\n message "+mail.message);
[https://docs.google.com/spreadsheets/d/12k5DF8rvuKex77B8uRWpx1FoMmSNaMdGvhNEEbXKCFc/edit?usp=sharing][1]
Apps script triggers cannot be trigerred by non manual input as specified in the documentation:
Script executions and API requests do not cause triggers to run. For example, calling Range.setValue() to edit a cell does not cause the spreadsheet's onEdit trigger to run.
What you can do instead is to set up a time-based trigger that scans your column 5 at regular interval and sends you an email if a cell contains 'Buy':
function sendEmailOnUpdate() {
var range = SpreadsheetApp.getActive().getRange('E2:E').getValues();
for (i = 0; i < range.length; i++) {
var cell = range[i];
if (cell == 'Buy') {
// Send email using Gmail
}
}
}
To set the trigger, head to Triggers and select time-based:
If you don't want to receive more emails for a cell you receive an alert for, make sure to make apps script change its value once the email has been sent.
Please I need your help here anyone. I was building a google form and I needed to add Geocode to the form so that users can send their location with the filled form. So I got this piece of code from Maxi Research (link). I did everything right but when it got to the point the user has to click the geolocation webapp link to send location coordinates I receive the error ("Exception: No HTML file named Index was found. (line 2, file "Code")"
I need help to recognize what's wrong in the code. Thanks
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
function getLoc(value) {
var destId = FormApp.getActiveForm().getDestinationId() ;
var ss = SpreadsheetApp.openById(destId) ;
var respSheet = ss.getSheets()[0] ;
var data = respSheet.getDataRange().getValues() ;
var headers = data[0] ;
var numColumns = headers.length ;
var numResponses = data.length;
var c=value[0];
var d=value[1];
var e=c + "," + d ;
// the script will add device geocode data in last submit data row by clicking the link on confirmation page that shows up after hitting the submit button
// geocode data here consists of 3 columns : time to click the link, longitude & latitude and address (using reverse geocode)
// as long as clicking the link is done before next respondent submit button, data will be entered in the right row.
// however particularly for the case of multiple devices that submit data at about the same time then whichever device clicks the link closer to the last submit row timestamp, their geodata will be entered in the last submit data row.
// leaving geocode column in the row above empty. In this case the geocode data will be in red font
// If sometime later another device click the link then the geodata will be entered in closest missing geodata row. The data will also be in red font
// Therefore for red font data you may want to check manually after completion for correct geodata entry. In the questionnaire add question about address to help checking.
if (respSheet.getRange(1,numColumns).getValue()=="GeoAddress") {
//fill data for second respondents onwards no missing geo data
// time here is Jakarta, you may need to change time to your local time (in GMT)
if (respSheet.getRange(numResponses,numColumns-2).getValue()=="" && respSheet.getRange(numResponses-1,numColumns-2).getValue()!="" ){
respSheet.getRange(numResponses,numColumns-2).setValue(Utilities.formatDate(new Date(), "GMT+7", "MM/dd/yyyy HH:mm:ss"));
respSheet.getRange(numResponses,numColumns-1).setValue(e);
var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]);
f= response.results[0].formatted_address;
respSheet.getRange(numResponses,numColumns).setValue(f);
}
//fill data with previous geo data missing. red font
else if (respSheet.getRange(numResponses,numColumns-2).getValue()=="" && respSheet.getRange(numResponses-1,numColumns-2).getValue()=="" ){
respSheet.getRange(numResponses,numColumns-2).setValue(Utilities.formatDate(new Date(), "GMT+7", "MM/dd/yyyy HH:mm:ss")).setFontColor("red");
respSheet.getRange(numResponses,numColumns-1).setValue(e).setFontColor("red");
var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]);
f= response.results[0].formatted_address;
respSheet.getRange(numResponses,numColumns).setValue(f).setFontColor("red");
}
//to fill missing previous data. red font
else if (respSheet.getRange(numResponses,numColumns-2).getValue()!=""){
for (i = 0; i < numResponses; i++) {
if (respSheet.getRange(numResponses-i,numColumns-2).getValue()=="") {
respSheet.getRange(numResponses-i,numColumns-2).setValue(Utilities.formatDate(new Date(), "GMT+7", "MM/dd/yyyy HH:mm:ss")).setFontColor("red");
respSheet.getRange(numResponses-i,numColumns-1).setValue(e).setFontColor("red");
var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]);
f= response.results[0].formatted_address;
respSheet.getRange(numResponses-i,numColumns).setValue(f).setFontColor("red");
break; }
}
}
}
else if (respSheet.getRange(1,numColumns).getValue()!="GeoAddress") {
//create labels in first row
respSheet.getRange(1,numColumns+1).setValue("GeoStamp");
respSheet.getRange(1,numColumns+2).setValue("GeoCode");
respSheet.getRange(1,numColumns+3).setValue("GeoAddress");
//fill data for first respondent
if (numResponses==2) {
respSheet.getRange(numResponses,numColumns+1).setValue(Utilities.formatDate(new Date(), "GMT+7", "MM/dd/yyyy HH:mm:ss"));
respSheet.getRange(numResponses,numColumns+2).setValue(e);
var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]);
f= response.results[0].formatted_address;
respSheet.getRange(numResponses,numColumns+3).setValue(f);
}
else if (numResponses > 2){
respSheet.getRange(numResponses,numColumns+1).setValue(Utilities.formatDate(new Date(), "GMT+7", "MM/dd/yyyy HH:mm:ss")).setFontColor("red");
respSheet.getRange(numResponses,numColumns+2).setValue(e).setFontColor("red");
var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]);
f= response.results[0].formatted_address;
respSheet.getRange(numResponses,numColumns+3).setValue(f).setFontColor("red");
}
}
}
<!DOCTYPE html>
<html>
<script>
(function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
})()
function showPosition(position) {
var a= position.coords.latitude;
var b= position.coords.longitude;
var c=[a,b]
getPos(c)
function getPos(value) {
google.script.run.getLoc(value);
}
}
</script>
<body>
<p> GeoCode Entered </p>
</body>
</html>
Maybe this works:
https://www.youtube.com/watch?v=J93uww0vMFY
Tutorial on Geotagging (Geo-stamp and Time-stamp) google forms. Add info on Latitude, Longitude, and Address (Street name and number, city, state, zip code, and country) of a device submitting google forms. Linking user’s location within google forms. Google forms integration with google maps.
This is my final project in view mode :
https://drive.google.com/open?id=1gOOb3hND3q2v0vTy8SfPVHFiCnTkJPR5
each row corresponds to a Google Doc project so when I modify in the row it will modify the corresponding Doc and when I click to see the document in the same window when I modify if then click the submit button I will change the row so I have an interactivity 100% with only the trigger onEdit(e) which will call function Edit(e) and openDialog(e).
The problem is that it can overwrite the Google Doc file as in the image. The problem comes from function
onEdit(e){
Edit(e);
openDialog(e); // tick to see the project in the same window and modify it transfer to the row
}
if I put openDialog(e); in comment if for instance I delete a text field in the row it will modify it correctly and with the openDialog(e) it will overwrite it. I've tried to put the openDialog code into the Edit(e) code as in the New.gs file but it's unsuccesful and close the doc https://script.google.com/d/1bV_eJONvUAbHyO6OB04atfm_sb5ZO8LWNoQ23fxf0lFnRzHRSwW7hsQc/edit?usp=sharing Do you have an idea to solve it? Thank you very much :) Separately they function very well. Together no it can overwrte the file so I don't know I need to close the Google Doc at very time because I open it 2 times?
It's the column J; I set a trigger to view the Google Doc in the same window and I can modify inside submit button and will change the row (this is the openDialog corresponding part)
Edit : sorry this is the code
var TITLE = 'Show Google Doc';
var SPREADSHEET_ID = "17ssKkCAoPUbqtT2CACamMQGyXSTkIANnK5CjbbZ1LZg"; // = assign your spreadsheet id to this variable
var column_name_project ;
var column_code_project ;
var column_chef_project;
var column_service ;
var column_other_services ;
var column_type_of_project ;
var column_perimeter ;
var column_date_project ;
var COLUMN_URL ;
var COLUMN_VIEW_Google_Doc;
/** will return the number of the column correspondant **/
function find_columns_in_projet(){
//search the columns
}
function onEdit(e){
Edit(e);
// openDialog(e);
}
function Edit(e) {
find_columns_in_projet();
var tss_bis = SpreadsheetApp.openById(SPREADSHEET_ID);
var sheet_bis = tss_bis.getSheets()[0];
var numRows_bis = sheet_bis.getLastRow();
var lastColumn_bis = sheet_bis.getLastColumn();
//from the second line car the first line we have the headers
var data_sheet = sheet_bis.getRange(1,1,numRows_bis,lastColumn_bis).getDisplayValues();
//Access the range with your parameter e.
var range = e.range;
var row = range.getRow();
var column = range.getColumn();
if( e.range.getColumnIndex() != COLUMN_URL + 1 ) {
var URL = data_sheet[row-1][COLUMN_URL-1];
Logger.log('Le URL est bien : ' , URL);
var body = DocumentApp.openByUrl(URL).getBody();
Logger.log('The body is ' + body );
if(body)
{... code to write the information from the spreadsheet to the Google Doc
}
}
}
/** every row in the current Sheet corresponds to a Google Doc Document **/
/** to see the Google Doc in the same page click in colum J and be able to modify the Google Doc 8 rows table inside
By clicking the button Submit it will transfer the information with what you have changed to the row of the corresponding project int the Sheet **/
function openDialog(e) {
/** columns in the Spreadsheet that are lines in the Google Doc table **/
/** find_columns_in_projet();
/** the good Sheet **/
if( ( e.range.getSheet().getName() === "Sheet1" ) && ( e.range.getColumnIndex() == COLUMN_VIEW_Google_Doc ) ) {
if( e.value === "TRUE" ) {
try {
//Get Google Doc body
/** the URL that is in the column I **/
var URL = e.range.offset(0,-1,1,1).getValue();
e.range.setValue("FALSE");
// Add this line
var ui = HtmlService.createTemplateFromFile('ModeLessDialog');
ui.body = URL; // Pass url to template
ui.insert = e.range.getRow() ;
ui = ui.evaluate().setWidth(1000).setHeight(500);
SpreadsheetApp.getUi().showModalDialog(ui, 'Show Google Doc');
}
catch(err) {
Logger.log(err);
}
}
}
}
function submitDoc(url,insert) {
/** the Spreadsheet need for getRange insert position **/
var tss_bis = SpreadsheetApp.getActiveSpreadsheet();
var sheet_bis = tss_bis.getSheets()[0];
find_columns_in_projet();
try {
Logger.log(url);
var google_doc = DocumentApp.openByUrl(url) ;
var body = google_doc.getBody();
if(body) {
code to write the information from the Google Doc into the Spreadsheet by button submit
}
Logger.log(body);
}
return true;
}
catch(err) {
Logger.log(err);
}
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<iframe id="srcFrame" src="<?= body ?>" name="<?= insert ?>" width="1000" height="400"></iframe>
<input type="button" value="Sumit changes to the Spreadsheet" onclick="submitDoc()">
<script>
function submitDoc() {
var url = document.getElementById("srcFrame").src;
var insert = document.getElementById("srcFrame").name;
google.script.run.submitDoc(url,insert);
google.script.host.close();
}
</script>
</body>
</html>
I am looking for a solution to a novel problem I have encountered in applying google apps scripting to, specifically, the google form product.
Context
The company I work for currently performs Quality Assurance(QA) on software we create for our clients by sending feedback through email.
This software is composed of "Parents" and their "Children". I was asked to look into using Google Forms as a method of creating QA feedback for each piece of software created.
I was able to get very far along in this process leveraging the Google Apps Script documentation. However, I have hit a knowledge barrier when it comes to implementing this in the wild.
Problem
I have one script attached to a very basic form that asks for the Name of the Tool(how we track our QA requests), Name of the Parents, and the name of the children for this software. (Currently I am asking for email as well for ease, but will soon replace with the automatic email grabbing function google apps script has).
This script takes in the responses to this first form and creates a new one using the responses. Now, for building purposes, I have created a second google apps script in the script editor of a form that was created by a submission of the first form. This script takes in the responses of to this second form and creates a third (I know, "formception" right?).
After building all this out and being fairly satisfied with my results I realized a massive error in my thinking. Outside of testing purposes users will be making many new forms from that first one. Each of these new forms will not have the google apps script, that I created for the second form, associated with them and as such will not generate the needed third form.
I am know seeking help identifying a method that will let the code I have written for the second form be automatically added to each new form the first creates. If this is not possible, I am seeking any alternatives. I have considered methods of containing the second google apps script within the first's codebase but I could not find a way to trigger that function on submission of the second form from within the first's script. Any ideas or approaches to consider would be very much appreciated.
Code:
As a note; I do realize this code is a bit messy and very redundant. This was hacked together as a brief proof of concept. I plan to clean it up and modularize it if I can find a solution to the issue above. Before wasting time on that though, I want to determine if what I am trying to do is possible within the limits of Google Apps Script.
First Script
//A function to run this unweildy Formception beast
//Its set to be run on a submission event of the original "First QA Form" which resides in ********'s Drive -> QA -> Dynamic Google Form Project Folder
function onSubmit() {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
//this whole loop just puts the responses into nested arrays
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
Logger.log('Response #%s to the question "%s" was "%s"',
(i + 1).toString(),
itemResponse.getItem().getTitle(),
itemResponse.getResponse());
}
}
//here we make another Form
var nextForm = FormApp.create('itemResponses[0].getResponse()');
//here we make a section for the questions that apply to the Tool as a whole
var generalSection = nextForm.addSectionHeaderItem();
generalSection.setTitle(itemResponses[0].getResponse());
//here we give the general section a checkbox item
var checkbox = nextForm.addCheckboxItem();
checkbox.setTitle('Which platforms did you test?');
checkbox.setChoices([
checkbox.createChoice('Chrome'),
checkbox.createChoice('FF'),
checkbox.createChoice('Safari'),
checkbox.createChoice('Vivaldi (Jokes)')
])
checkbox.setRequired(true);
//here we give the general section a multiple choice question and make it required
var generalLooks = nextForm.addMultipleChoiceItem()
.setTitle('Does this campaign look good in general?')
.setChoiceValues(['Yes','No'])
.setRequired(true);
//here we give the general section a place for comments
var generalComment = nextForm.addParagraphTextItem()
.setTitle('General comments:')
.setHelpText('Separate each comment with a return.')
.setRequired(false);
//here we give the general section a place for images to be submitted
var generalImg = nextForm.addParagraphTextItem()
.setTitle('General comment reference image links:')
.setHelpText('Separate each image link with a return.')
.setRequired(false);
//here we create a new section to conatin all the parents
var parentPage = nextForm.addPageBreakItem();
parentPage.setTitle(itemResponses[0].getResponse() + '| Parent(s)');
//here we create an array to conatain all the parent names
var parents = [{}];
//we populate this array with the responses to the second question of the "First QA Form" which asked for ther Parent names seperated by returns
parents = itemResponses[1].getResponse().split("\n");
//this for loop creates a section and series of questions related to each parent
for (var p = 0; p < parents.length; p++) {
//adds a section for each parent
var parentSection = nextForm.addSectionHeaderItem().setTitle(parents[p]);
//adds a yes or no question for each parent
var parentLooks = nextForm.addMultipleChoiceItem()
//sets the name of the question dynamically using the current parent
.setTitle('Does ' + parents[p] + ' look good in general?')
.setChoiceValues(['Yes','No'])
.setRequired(true);
//adds a comment section for each
var parentComment = nextForm.addParagraphTextItem()
.setTitle(parents[p] + ' comments:')
.setHelpText('Separate each comment with a return.')
.setRequired(false);
//adds an img section for each (there is potential to get into regex here and verify links)(there is also potential to replace with apps script UI stuff for direct upload)
var parentImg = nextForm.addParagraphTextItem()
.setTitle(parents[p] + ' image links:')
.setHelpText('Separate each image link with a return.')
.setRequired(false);
}
//end for loop
//makes a new page for the children
var childPage = nextForm.addPageBreakItem();
childPage.setTitle(itemResponses[0].getResponse() + '| Children');
var children = [{}];
children = itemResponses[2].getResponse().split("\n");
//this for loop creates a section and series of questions related to each child
for (var c = 0; c < children.length; c++) {
var childSection = nextForm.addSectionHeaderItem().setTitle(children[c]);
var parentSelect = nextForm.addListItem().setRequired(true);
parentSelect.setTitle('Which parent does this child belong to?');
parentSelect.setChoiceValues(parents);
//adds a yes or no question for each parent
var childrenLooks = nextForm.addMultipleChoiceItem()
.setTitle('Does ' + children[c] + ' look good in general?')
.setChoiceValues(['Yes','No'])
.setRequired(true);
//adds a comment section for each
var childrenComment = nextForm.addParagraphTextItem()
.setTitle(children[c] + ' comments:')
.setHelpText('Separate each comment with a return.')
.setRequired(false);
//adds an img section for each (there is potential to get into regex here and verify links)(there is also potential to replace with apps script UI stuff for direct upload)
var childImg = nextForm.addParagraphTextItem()
.setTitle(children[c] + ' image links:')
.setHelpText('Separate each image link with a return.')
.setRequired(false);
}
//end for loop
//we need the email of the account manager we want this to go to after we fill it out
var finalStep = nextForm.addSectionHeaderItem();
finalStep.setTitle('Final Step');
//this is a response field that grabs the email of the account manager, it is required.
var accountEmail = nextForm.addTextItem();
accountEmail.setTitle('What is the email of this account manager?').setRequired(true);
//grabs the form we just made's ID
var id = nextForm.getId();
//create the link that will be sent to the QAer to respond with content and images
var emailBody = 'https://docs.google.com/a/***********.com/forms/d/' + id + '/viewform';
//set the email of the QAer
var email = itemResponses[3].getResponse();
//set the subject of the email to the name of the Tool
var emailSubject = itemResponses[0].getResponse();
//send the email of the link to the new form to the QAer
MailApp.sendEmail({
to: email,
subject: emailSubject,
htmlBody: emailBody});
Second Form Script
//set to be run on a submission event of the second form "Next QA Form" which resides in ********'s Drive
function onLastSubmit() {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
//loop just puts the current responses into nested arrays
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
// Logger.log('Response #%s to the question "%s" was "%s"',
// (i + 1).toString(),
// itemResponse.getItem().getTitle(),
// itemResponse.getResponse());
}
}
//create a Form instance of our last(old) form. It will be usefull in accessing data like parent and child names
var previousForm = FormApp.openById('***********************');
var oldFormResponses = previousForm.getResponses();
//loop just puts the old responses into nested arrays
for (var i = 0; i < oldFormResponses.length; i++) {
var oldFormResponse = oldFormResponses[i];
var oldItemResponses = oldFormResponse.getItemResponses();
for (var j = 0; j < oldItemResponses.length; j++) {
var oldItemResponse = oldItemResponses[j];
// Logger.log('Response #%s to the question "%s" was "%s"',
// (i + 1).toString(),
// oldItemResponse.getItem().getTitle(),
// oldItemResponse.getResponse());
}
}
//some debugging and such
Logger.log(oldItemResponses[0].getResponse());
Logger.log(itemResponses[4].getResponse());
//oldItemResponses[0] = Name of Tool
var toolName = oldItemResponses[0].getResponse();
Logger.log(toolName);
//oldItemResponses[1] = parent names
var parentNames = oldItemResponses[1].getResponse();
Logger.log(parentNames);
//oldItemResponses[2] = child names
var childNames = oldItemResponses[2].getResponse();
Logger.log(childNames);
//oldItemResponses[3] = email of the QAer
var qaEmail = oldItemResponses[3].getResponse();
//newItemResponse[0] = tested platforms
var testedPlatforms = itemResponses[0].getResponse();
//make the last form
var lastForm = FormApp.create('Account Manager Response | ' + toolName);
//make a section for the general content
var generalSection = lastForm.addSectionHeaderItem();
generalSection.setTitle(toolName + ' | General Section');
//make a checkbox item for the CD to approve each of the platforms that the QAer says were tested
var testedCheckbox = lastForm.addCheckboxItem();
testedCheckbox.setTitle('If you agree a platform was accurately tested please check it off below.');
//use the array from the first response of the previous form (platforms that were tested) to generate a list of the tested platforms for the CD to approve
if ( Array.isArray(testedPlatforms)) {
testedCheckbox.setChoiceValues(testedPlatforms);
} else {
testedCheckbox.createChoice(testedPlatforms);
}
//set general section response variables
var genYesNo = itemResponses[1].getResponse();
var genComments = itemResponses[2].getResponse();
var genImgs = itemResponses[3].getResponse();
//if statement either says the general section looks good or makes a bunch of fields with the content the QAer left
if ( genYesNo == 'Yes') {
generalSection.setHelpText('Looks Good!')
} else {
//make a checkbox item for the CD to approve or not approve the general section QA feedback
if ( genComments != '') {
var generalCheckbox = lastForm.addCheckboxItem();
generalCheckbox.setTitle(toolName + ' | General Information and Comments');
generalCheckbox.setHelpText('Please check the boxes that you have fixed. Feel free to leave a note about any in the following section.');
if ( Array.isArray(genComments)) {
generalCheckbox.setChoiceValues(genComments);
} else {
generalCheckbox.createChoice(genComments);
}
}
//create a for loop to display image items for any linked images that were included by the QAer in the general section
if ( genImgs != '') {
if ( Array.isArray(genImgs)){
for (var gI = 0; gI < genImgs.length; gI++) {
var generalImg = lastForm.addImageItem();
generalImg.setTitle('General Section | Image ' + (gI + 1));
var genImg = UrlFetchApp.fetch(genImgs[gI]);
generalImg.setImage(genImg);
}
} else {
var generalImg = lastForm.addImageItem();
generalImg.setTitle('General Section | Image 1');
var genImg = UrlFetchApp.fetch(genImgs);
generalImg.setImage(genImg);
}
}
}
//make a paragraphTextItem for the CD to leave notes about this section if they would like
var generalNotes = lastForm.addParagraphTextItem()
.setTitle('Notes about the general section:')
.setHelpText('Leave notes here about any items you have not fixed and other things you would like the QAer to know.');
//make a new page for the parent content
var parentPage = lastForm.addPageBreakItem();
parentPage.setTitle(toolName + ' | Parent(s)');
//a variable that we can increment by 2 to account for there being 3 items in each parent
var incParent = 0;
//a loop that creates items for each parent including: new section, checkbox to approve content and image displays
for (var i = 0; i < parentNames.length; i++) {
var parYesNo = itemResponses[(i + incParent) + 5].getResponse();
var parComments = itemResponses[(i + incParent) + 5].getResponse();
var parImgs = itemResponses[(i + incParent) + 5].getResponse();
//create the new section for each parent
var parentSection = lastForm.addSectionHeaderItem();
//and name it
parentSection.setTitle(parentNames[i]);
//if statement to ensure we dont show any content if the QAer checked 'Yes' for looks good
//using incOne to ensure
if (parYesNo == 'Yes') {
parentSection.setHelpText('Looks Good!');
} else {
//create a checkbox list for all the comments the QAer listed if they clicked 'No' for looks good
if (parComments != '') {
var parentCheckbox = lastForm.addCheckboxItem();
parentCheckbox.setTitle(parentNames[i] + ' | QA Comments');
parentCheckbox.setHelpText('Please check the boxes that you have fixed. Feel free to leave a note about any in the following section.');
if (Array.isArray(parComments)) {
parentCheckbox.setChoiceValues(parComments);
} else {
parentCheckbox.createChoice(parComments)
}
}
}
//create the images the QAer listed if they clicked 'No' for looks good
if (parImgs != '') {
if (Array.isArray(parImgs)) {
for (var pI = 0; gI < parImgs.length; pI++) {
var parentImg = lastForm.addImageItem();
parentImg.setTitle(parentNames[i] + ' | Image ' + (pI + 1));
var parImg = UrlFetchApp.fetch(parImgs[pI]);
parentImg.setImage(parImg);
}
} else {
var parentImg = lastForm.addImageItem();
parentImg.setTitle(parentNames[i] + ' | Image ');
var parImg = UrlFetchApp.fetch(parImgs[pI]);
parentImg.setImage(parImg)
}
}
//increment to account for the other items in each parent
incParent += 2;
}
//end for loop
//make a new page for the children content
var childPage = lastForm.addPageBreakItem();
childPage.setTitle(toolName + ' | Children');
//determine how many parents there are and count three items for each
//also account for the items from the general section (4 items)
var parentItems = parentNames.length * 3;
var nonChildItems = parentItems + 4;
//a variable that we can increment by 4(the number of items in each child)
var incChild = 0;
//creates items for each parent including: checkbox to approve content and image displays
for (var j = 0; j < childNames.length; j++) {
var chiYesNo = itemResponses[nonChildItems + (j + incChild)].getResponse();
var chiComments = itemResponses[(j + incChild) + nonChildItems].getResponse();
var chiImgs = itemResponses[(j + incChild) + nonChildItems].getResponse();
//create sections for each child
var childSection = lastForm.addSectionHeaderItem();
childSection.setTitle(childNames[j] + ' | ' + itemResponses[nonChildItems + (j + incChild + 1)].getResponse());
if (chiYesNo == 'Yes') {
childSection.setHelpText('Looks Good!');
} else {
//create a checkbox list for all the comments the QAer listed if they clicked 'No' for looks good
if (chiComments != '') {
var childCheckbox = lastForm.addCheckboxItem();
childCheckbox.setTitle(childNames[j] + ' | QA Comments');
childCheckbox.setHelpText('Please check the boxes that you have fixed. Feel free to leave a note about any in the following section.');
if (Array.isArray(chiComments)) {
childCheckbox.setChoiceValues(chiComments);
} else {
childCheckbox.createChoice(chiComments);
}
}
}
//create the images the QAer listed if they clicked 'No' for looks good
if (chiImgs != '') {
if (Array.isArray(chiImgs)) {
for (var cI = 0; cI < chiImgs.length; cI++) {
var childImg = lastForm.addImageItem();
childImg.setTitle(childNames[j] + ' | Image ' + (cI + 1));
var chiImg = UrlFetchApp.fetch(chiImgs[cI]);
childImg.setImage(chiImg);
}
} else {
var childImg = lastForm.addImageItem();
childImg.setTitle(childNames[j] + ' | Image ');
var chiImg = UrlFetchApp.fetch(chiImgs[cI]);
childImg.setImage(chiImg);
}
}
//increment to account for the other items in each child
incChild += 3;
}
//end for loop
//grabs the form we just made's ID
var id = lastForm.getId();
//create the link that will be sent to the QAer to respond with content and images
var emailBody = 'https://docs.google.com/a/**************.com/forms/d/' + id + '/viewform';
//set the email of the QAer
var email = qaEmail;
//set the subject of the email to the name of the Tool
var emailSubject = toolName + ' | CD Response';
//send the email of the link to the new form to the CD
MailApp.sendEmail({
to: email,
subject: emailSubject,
htmlBody: emailBody});
}
Thanks in advance!
*edit for company privacy reasons.
I've been doing something very similar and have mostly been successful.
I was able to ensure code is moved over to newly created forms by creating a blank template form, with the necessary script attached.
When when a new form is needed with this script, I create a copy of the template document and then populate this with the necessary contents.
The only problem I have run into with this is being unable to easily set up triggers for code to run on form submission in these new forms. I have solved this by prompting the user to open the newly created form and click on a menu item I have added to 'initialise permissions'.
Unfortunately there is no way to programmatically attach a script to a form. In general, if you expect a script to be used on multiple forms, docs, etc, it's best to convert it to an add-on. This has the benefit of allowing you to make updates to the script over time, instead of each being a local copy.
Forms making forms making forms is also probably an anti-pattern. What you probably need is a more complex web app, which you can build in Apps Script but is quite a bit more involved.