I have a problem to update the values on the fields of a form. I cannot update the date in its respective text box and the spreadsheet that receives the values only shows the values after i refresh the page (both Ui and spreadsheet are embbeded in the Google Sites)
The var date, explicited as a global var
var date=new Date();
The TextBox
var DataTextBox=app.createTextBox().setId('Data').setName('Data').setValue(date).setReadOnly(true).setText(Utilities.formatDate(date, 'GMT -02:00', 'dd/MM/yyyy HH:mm:ss'))
The handler
var clickHandler2=app.createServerHandler('updateAll');
button.addClickHandler(clickHandler2);
clickHandler2.addCallbackElement(panel);
The function (have others fields updated, but they are working fine)
function updateAll(e) {
//DataTextBox.setValue(date).setReadOnly(true).setText(Utilities.formatDate(date, 'GMT -02:00', 'dd/MM/yyyy HH:mm:ss'));
var newDate=date;
app.getElementById("Data").setValue(newDate);
app.getElementById("TicketId").setValue("");
app.getElementById("TicketType").setValue(TicketTypeArray[0]);
app.getElementById("DemandedBy").setValue(DemandedByArray[0]);
app.getElementById("Analyst").setValue(AnalystArray[0]);
app.getElementById("Description").setValue("");
}
How do i update the date field and how can i make the spreadsheet shows the last data submited by the form?
You have to add 'return app' in order to show updated fields in gui.
Related
I have a column with date
var range = sheet.getRange(1,1);
range.copyTo(sheet.getRange(2,1));
give me the same date as previous row.
The above code works well for values and formulas, but not date.
How do I copy it such that it replicate the dragging behaviour, i.e. 11/6/2021 become 12/6/2021.
I have already formatted it as date, I can increment it by manually dragging the cell in UI, but I can't use copyTo in app script to achieve the same result.
In Apps Script, you need to convert to cell values to Date format in order to increment the date as google sheet is using Java Script by adding customize function, below is the way you can add Day + 1 in the next row using Javascript new Date() method:
function increRow(){
var ss = SpreadsheetApp.getActiveSheet();
var date = new Date( ss.getRange(1,1).getValue());
ss.getRange(2,1).setValue(new Date(date.setDate(date.getDate() + 1)));
}
You may try another method autofill which perform as human action to fill Range("A2:A4"), if you preferred:
function autoFill() {
var spreadsheet = SpreadsheetApp.getActiveSheet();
spreadsheet.getRange(2,1).autoFill(spreadsheet.getRange(2,1,3,1), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
};
I am building a multi-section questionnaire (3 in total) and I want to have 4 sheets to hold the data (one master and one for each section).
How would I send data to another sheet from the master spreadsheet when a new row is added to the Master Sheet and make it dynamic so it does not pull the same row everytime?
I have found this script online:
script link
but it is for moving data between sheets unfortunately. Could it be remodeled?
Thanks!
Perhaps the setFormula class can help you, it can be dynamic & you can update it with a trigger
sheet.getRange(1,1,1,1).setFormula('=IMPORTRANGE("SPREADSHEET_URL", "SHEET_NAME!'+rangevariable1inA1notation+':'+rangevariable2inA1notation+'")');
Make sure you get the range you want, and the notation of those 2 variables to get the dynamic range:
var rangevariable1inA1notation = sheet.getRange(1,1).getA1Notation(); // for example, instead of 1,1 it could be your own variable
var rangevariable2inA1notation = sheet.getRange(2,2).getA1Notation(); //for example
To send values to another sheet (besides from your main one), you can use the onFormSubmit function, it will be triggered every time the form is submitted and with its event object, which contains the information from your form, you will be able to pass those values into the other sheets as you want.
// This will be triggered every time the form is submitted
function onFormSubmit(e) {
// Get all sheets in the active spreadsheet
var sheetsArr = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// Get the second sheet
var slaveSheet1 = sheetsArr[1];
// Get the row where the values will be inserted
var rowVals = slaveSheet1.getLastRow() + 1;
// The number of cols where you will puth the values
var numberOfCols = e.values.length;
// Set values that came from the form
slaveSheet1.getRange(rowVals, 1, 1, numberOfCols).setValues([e.values]);
}
Be careful with the installable triggers Restrictions.
I'm trying to pre fill the date in google form. so It will put the date of the day it filled as default.
I tried using the TODAY() function in google sheets . so i change the url to get the date from cell in google sheets . works for everything except the Date..
https://docs.google.com/forms/d/e/1FAIpQLSf7cdT34eHp-GXagq3DsnxX1MD_c-G6lbF6yFWOMnUvtPYUUQ/viewform?entry.178275308=**&A6&**
Any idea how can i do it in the simplest way?
Deploy your script as a Web App
Publish your Apps Script as a Web App and provide the URL of the web deployment to the users
Write your code inside a doGet() function, so it gets evaluated every time a user open the link
Get the current date with new Date() and pass it as an itemResponse to a prefilled Form
Create a redirection to the URL to the prefilled form with the Java Script method window.top.location.href inside of <script> </script> tags and return it with HtmlService
// will be automatically run every time the user opens the URL of the web deployment
function doGet(){
//open your form
var form = FormApp.openById('1Zs5Wo0wS9esNkcq8ztOZmQs3f5NrO7hgJ_0fYk262z4');
//get the questions
var questions = form.getItems();
//get the question where you want to prefill the date as an answer, e.g. first question
var question1=questions[0];
//get the current date
var time=Utilities.formatDate(new Date(), "GMT+2", "dd-MM-yyyy");
// prefill form with current date
var prefilledTime=form.createResponse().withItemResponse(question1.asTextItem().createResponse(time));
// Get the link to the prefilled form
var URL=prefilledTime.toPrefilledUrl();
// redirect to the prefilled form URL dynamically created above
return HtmlService.createHtmlOutput("<script>window.top.location.href=\"" + URL + "\";</script>");
}
If you just want the date the Form is submitted, Google Forms provides this automatically in it's Timestamp.
If you want to pre-fill a date from an existing spreadsheet, then provided you obey Google's date-format, you can use the URL approach (months & days must be 2 digits i.e. "01" not "1").
=substitute('Sheet_with_Form_Link'!$A$1,"2099-01-01",text(Sheet_with_date!A1,"yyyy-mm-dd"))
WhereSheet_with_Form_Link'!$A$1 is a cell in a Sheet in a Google SpreadSheet containing the URL obtained via 'Get Prefilled Link'
"2099-01-01" is the data you Pre-filled into the Form whilst using 'Get Pre-filled Link'
Sheet_with_date!A1 is a cell in a Sheet in a Google SpreadSheet containing your desired date e.g. one of
=today()
1920/03/26
17Oct2002
I took the code from ziganotschka as base, but I had to make some changes, maybe it helps somebody:
function doGet(){
//open your form open by URL
var form = FormApp.openByUrl('https://docs.google.com/forms/d/1M-zgHyDOz2ob5StkwIoDCShJ9tePw-I5TGhdx/prefill')
//get the questions
var questions = form.getItems();
//get the question where you want to prefill the date as an answer, e.g. first question
var question1=questions[0];
//get the current date
var time=new Date();
// prefill form with current date
var prefilledTime=form.createResponse().withItemResponse(question1.asDateItem().createResponse(time));
// Get the link to the prefilled form
var URL=prefilledTime.toPrefilledUrl();
// redirect to the prefilled form URL dynamically created above
return HtmlService.createHtmlOutput("<script>window.top.location.href=\"" + URL + "\";</script>");
}
And when you're done, don't forget to Publish -> Deploy as a Web App...
I have multiple Google Forms with identical response fields inputting into a certain Google Sheets, and there is no way of telling them apart other than their names and descriptions. Using Google Apps Script, is there any way I can add the name and/or description of the Google Form into the Sheets, along with everything that is normally added to the Sheets? I did a bit of research about something with "source" but I have no idea how to implement it.
Thanks!
You can accomplish this by going to your form in edit mode selecting "Script Editor" and pasting the below code into the script. You will need to click on the "Trigger" button (looks like a clock, if you can't find it follow here https://developers.google.com/apps-script/guides/triggers/installable). Click Add Trigger and select event type "On Form Submit"
function writeFormTitle(){
//Get Form
var form = FormApp.getActiveForm();
//Get Form's Title
var formTitle = form.getTitle();
//Get the Write To Spreadsheet ID where the data will be written to
var destinationSpreadsheet = form.getDestinationId();
//Activate that Spreadsheet by the destinationSpreadsheet ID
var sheet = SpreadsheetApp.openById(destinationSpreadsheet).getActiveSheet();
//Find the last column and row
var writeToColumn = sheet.getLastColumn()+1;
var writeToRow = sheet.getLastRow();
//Write title to Spreadsheet next to submitted data.
sheet.getRange(writeToRow, writeToColumn, 1, 1).setValue(formTitle);
}
This question already has an answer here:
Ensure form submission trigger runs only one sheet
(1 answer)
Closed 6 years ago.
I have a Google Sheets script which copies elements of a submitted form data to another spreadsheet and then transforms some of the data for separate use. The script piece works perfectly when I run it manually, but I want it to run automatically.
There are a number of forms connected to this spreadsheet, all submitting data at different times.
So, I sort of have two questions:
How do I get my script to run automatically? I have created a trigger for From Spreadsheet and On Form Submit, but it never runs.
How do I make sure it runs only when: either the specific form is submitted or when the specific sheet has a new row added?
There were a number of articles around the web which had pieces of an answer, but many were from 3 years ago, and it seems that the way Sheets, Forms and the Script Editor work have changed much since then.
You need an onFormSubmit function along with the
installable trigger.
If you are using a Html Service Form, you can get
the name of the destination sheet from the form event
output. You can't do that with forms created from
the spreadsheet. The best I have been able to do is
to compare the event timestamp to the timestamps on
the last rows of the form response sheets to determine which
sheet was updated. This is not totally fool proof since
it is possible for mulitiple forms to be submitted at
the same time (but not likely).
Try this:
//installable trigger also needed.
function onFormSubmit(e){
var ts=e.namedValues.Timestamp
findResponseForm(ts)
}
function findResponseForm(ts) {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getSheetByName('Form Responses 1') //get 'Form Responses 1' sheet
var lr=s.getLastRow() //get last row number of 'Form Responses 1' sheet
var s1=ss.getSheetByName('Form Responses 2') //get 'Form Responses 2' sheet
var lr1=s1.getLastRow() //get last row number of 'Form Responses 2' sheet
var time1=s.getRange(lr, 1, 1,1).getValue() //get 'Form Responses 1'last Timestamp
var ftime1=Utilities.formatDate(time1, "GMT-6", "d/M/yyyy' 'HH:mm:ss") //convert 'Form Responses 1'last Timestamp format to form Timestamp format
var time2=s1.getRange(lr1, 1, 1,1).getValue() //get 'Form Responses 2'last Timestamp
var ftime2=Utilities.formatDate(time2, "GMT-6", "d/M/yyyy' 'HH:mm:ss") //convert 'Form Responses 1'last Timestamp format to form Timestamp format
if(ts==ftime1){ //if date/time match
Logger.log('Form Responses 1')
//call function to prosses 'Form Responses 1' data
}
else if(ts==ftime2){ //if date/time match
Logger.log('Form Responses 2')
//call function to prosses 'Form Responses 2' data
//Add more 'else if' if more forms are involved.
}
}