Google Forms - Change date format for all respondents - date

In Google Forms the date format is changed according to Language settings in Google account of the respondent.
I want the date format to be the same for all respondents.
Can I put some code in Apps script?
I tried the following to prefill the date:
function doGet(){
var form = FormApp.openById('1esX110YGnCOK8SJcz3jrll9ZiZZAhiIlpHxCTYbGcMg');
var questions = form.getItems();
var question2=questions[0];
var time=Utilities.formatDate(new Date(), "GMT+2", "dd-MM-yyyy");
var prefilledTime=form.createResponse().withItemResponse(question2.asTextItem().createResponse(time));
var URL=prefilledTime.toPrefilledUrl();
return HtmlService.createHtmlOutput("<script>window.top.location.href=\"" + URL + "\";</script>");
}
Thank you in advance!

Related

I use the same code on different google sheet but the date format is different. How is it possible?

I'm facing a little problem. I'm using google sheet and google appscript to send email to customers. However when I use the same code on two different sheets the date format in the mail title changes.
One will give me
Facture Osteo n° 14752 du 27/06/2022
and the other (the wrong one)
Facture Osteo n°14752 du Mon Jun 27 2022 00:00:00 GMT+0200 (heure d’été d’Europe centrale)
To create the email I use data from a sheet and it replace data in an another sheet used to generate the invoice.
I checked the settings in the google sheet I don't see any issue with it, same timezone and country.
Anyone one as an idea?
function Facturereview2() {
var app = SpreadsheetApp;
var data=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
var currRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var i=currRow-1
var donneesFacture=data[i];
var doc=app.openById("1zGzIrT8AT0rGRfYR-qIOYJt-lB_IGLvSyh3BWPjKlxU")
var Facture = doc.getSheetByName("Facture");
Facture.getRange(2,4).setValue(donneesFacture[1]);
Facture.getRange(11,4).setValue(donneesFacture[2]);
Facture.getRange(13,5).setValue(donneesFacture[0]);
Facture.getRange(19,7).setValue(donneesFacture[3]);
SpreadsheetApp.flush();
var pdf=doc.getAs('application/pdf');
var file=DriveApp.createFile(pdf);
DriveApp.removeFile(DriveApp.getFileById(doc.getId()));
const HTMLTemplate = HtmlService.createTemplateFromFile("Facture review")
const HTMLforemail = HTMLTemplate.evaluate().getContent()
var email=donneesFacture[9]
var sujet="Facture Osteo n°"+donneesFacture[1]+" du "+donneesFacture[2]; // donneesFacture[2] is the date
var texte="Veuillez trouver ci attachée votre note d'honoraire du "+donneesFacture[2]+".\nText,\n\n Text";
var option={
htmlBody:HTMLforemail,
attachments:file
};
GmailApp.sendEmail(email,sujet,texte, option);
}
Replace .getValues() with .getDisplayValues() to get all data, including dates, as text strings in the format they show in the spreadsheet.

How to send a conditional email based on the value of a cell from a form response?

I have a formula that calculates a number based on the response from a google form. Depending on what this number I want to send an email using details from the from as well as a pre typed email in another cell.
In Col1 is a time stamp, in col14 is an employee start date. My formula in Col33 works out how many days they have been employed at the time of submitting the form.
I want to send an email to the person if the number of days is less than 182.
I have an email pre typed out and can place this anywhere. At the moment I have it in all cells in col36. The email address will be in column32.
I have tried a number of different codes and none of them are sending the email no matter what the trigger I have set up is. I have very basic knowledge on apps script so my current code might be completely wrong, but it should show roughly what I'm getting at.
function sendEmail() {
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
for (i in values.length) {
var data = values[i][33];
var emailAddress = values[i][32];
var message = values[i][36];
if (data < 182); {
MailApp.sendEmail(emailAddress, "Flexible Working Request", message);
}
}
}
The current results have just been deleting the data in col33, Col34 & Col36 on the new form response row only.
Sorry if this question has been answered elsewhere, any other answer I found to similar issues I could not get to work.
I got someone who is much better at google apps script at work to give me a hand
It is to do with google forms pushing down formulas to the side
So we had to move the formula calculating the number of days to another sheet and then used this formula which worked
function sendEmailv2() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form responses
1');
var scrip = Session.getScriptTimeZone();
var date = sheet.getRange(sheet.getLastRow(),14).getValue();
var sub = sheet.getRange(sheet.getLastRow(),1).getValue();
Logger.log(date);
var fortmat = Utilities.formatDate(new Date(date), scrip, "dd/MM/yyyy");
var Subfortmat = Utilities.formatDate(new Date(sub), scrip, "dd/MM/yyyy");
var emailAddress = sheet.getRange(sheet.getLastRow(),32).getValue();
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet4');
var message = sheet2.getRange(1,1).getValue();
var days = sheet2.getRange(sheet2.getLastRow(),2).getValue();
if (days<182){
MailApp.sendEmail(emailAddress, "Flexible Working Request", message,{noReply:true});
}
}
Thanks!
You don’t need to go over all the columns to get a single cell value, so there is no need for a for loop. You can do it directly with:
var sheet = SpreadsheetApp.getActiveSheet().getSheets[0];
var cell = ["A33"];
var days_value = sheet.getRange(cell).getValue();
Then you can just make an if condition to send the email:
if (days_value < 182){
MailApp.sendEmail(emailAddress, "Flexible Working Request", message);
}
Hope this helps

Determine correct date format of Google Form response

What is the best way to determine the correct date format (dd/mm or mm/dd) of a Google Form response.
When I use the namedValues object:
function onFormSubmit(e){
var namedValues = e.namedValues;
var date = namedValues['Date']; // Date=[05/06/2018]
var date = new Date(date);
Logger.log(date); //Sun May 06 00:00:00 GMT+10:00 2018
}
When I use the value from the spreadsheet:
function onFormSubmit(e){
var range = e.range;
var row = range.getRow();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Form Responses 1');
var date = sheet.getRange(row,2).getValue();
Logger.log(date); //Mon Jun 05 00:00:00 GMT+10:00 2018
}
I don't know whether I should be, but I am hesitant using the values from the spreadsheet in an onFormSubmit trigger, since I have experienced instability in the past where I think the trigger was running before the data was being posted to the spreadsheet.
I cannot find anything in the Google Forms documentation stating whether a date response is always in a consistent format. If it was always dd/mm/yyyy I could construct the date using the string parts.
Is there a way to use determine the correct date format from the namedValues object?
P.S I would rather not use the moment.js library for this one requirement, so keen to understand if its possible without.
You Google form will constantly submit the date in the same format, but your sheet might change the appearance of formatting.
Also, there is now a date format utility built into Apps script. You can change the date string to the format that you need.
// This formats the date as Greenwich Mean Time in the format
// year-month-dateThour-minute-second.
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
Logger.log(formattedDate);

Prefill current date in Google Form with Google Apps Script

I have a Google Form to a spreadsheet and I need a date field pre-filled with the current date.
Something like an "onOpen trigger", which updates the date field or a date field with now().
Is this possible in Google Apps Script?
In your case, some of the points of using the Form remain unclear. Suppose that you have the ability to edit a link to the Form or your users agree to add the bookmarklet to their browser.
Common code
let id='1FAIpQLScx-1H1moCzBfkTEOZnVgBScbJeHZ4YE5E6IY2mNZvMuVcOXA';
window.open(
`https://docs.google.com/forms/d/e/${id}/viewform?usp=pp_url&entry.1000000=`+
(new Date()).toISOString().split('T')[0]
);
Bookmarklet
Just add the next string to the bookmarks bar
javascript:let id = '1FAIpQLScx-1H1moCzBfkTEOZnVgBScbJeHZ4YE5E6IY2mNZvMuVcOXA'; window.open(`https://docs.google.com/forms/d/e/${id}/viewform?usp=pp_url&entry.1000000=` + (new Date()).toISOString().split('T')[0]);
I put a static HTML page on S3 with a few line of Javascript for redirecting. See https://jsfiddle.net/barryku/3etd8qf0/
var formId = "1FAIpQLSc_acGSzp2VeJIyG0tNJwqcNdUPOweLROGenY0Fe56I635VGQ";
var dateField = "entry.1608430301";
var formsUrl = "https://docs.google.com/forms/d/e/"+ formId + "/viewform?usp=pp_url&" + dateField + "=$$today&entry.747437339=Yes";
var today = new Date();
var redirectUrl = formsUrl.replace("$$today", new Date(today.getTime() - (today.getTimezoneOffset() * 60000)).toISOString().split('T')[0]);
window.location.replace(redirectUrl);

Read emails on specific date using google app script

Hello guys I am working on a project where I want to read emails from gmail inbox on a specific date. I have referred the official documentation but it hasn't been much helpful to me.
var master = SpreadsheetApp.openById(<SPREADSHEET_ID>);
var demosheet = master.getSheetByName("demo");
var newDate = Utilities.formatDate(new Date(), "GMT+5:30", "dd/MM/YYYY");
var getdate = demosheet.getDataRange().getValues();
var firstThread = GmailApp.getInboxThreads(0,1)[0];
//to get date from spreadshet
for(i=2;i<getdate.length;i++)
{
Logger.log(firstThread.getLastMessageDate(getdate[i][0])); }}
Can anyone please help, thanks in advance.
Look at GmailApp.search. Google for search format as its not in the docs. Probably uses syntax 'before:x after:y
https://developers.google.com/apps-script/reference/gmail/gmail-app?hl=ja#search(String,Integer,Integer)