How to change date format in Google Forms Response? - datetime

I have a Google Forms to get my delivery order from customer and on the forms I have Date field.
The response of the forms will be filled automatically to order document per response.
I use this scripts:
function autoFill(e) {
var timestamp = e.values[0];
var nama = e.values[1];
var tglBuat = e.values[10];
var file = DriveApp.getFileById(MY_TEMPLATE_FILE_ID);
var folder = DriveApp.getFolderById(OUTPUT_FOLDER_ID);
var copy = file.makeCopy(nama+"_"+timestamp, folder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
body.replaceText('#NamaLengkap#', tglBuat);
body.replaceText('#TanggalDibuat#', tglBuat);
doc.saveAndClose();
}
The flow is simple like this:
I prepared Template file for the Order Document paper
Customer will fill the forms
Form result will be kept in certain Google Spreadsheet
The script above on the (3), will be triggered everytime (2) submitted
Voila, I have Order Document filled with customer order details
My template file are something like this:
Customer Name: #NamaLengkap#
Order Date: #TanggalDibuat#
My problem is here in date format, I want the output on my template file using this format "26 August 2020", but the google form only give this format "08/26/2020".
How do I changes it?
I read some article about changing the email format before filling the form, but i don't think this is good solution. Because customer wont care at all.

Solution
You just need to take your date and convert it to a Date String Javascript object as shown below:
function myFunction() {
var shortDate = new Date("03/25/2015");
var longDateFormat = shortDate.toDateString();
Logger.log(longDateFormat);
}
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

Related

Is there a timestamp feature in GTM event data?

I'm creating an event data via GTM, and typically if it's sent to GA the timezone will follow the GA timezone but if I sent it to a different endpoint how do I capture the timestamp for the GTM event? Would I have to populate it in the dataLayer?
You may create a custom variable using javascript. That custom variable will return the current timestamp (beware that you may need to deal with browsers timezones, if that is important to you).
Go to Variables and add a new using the type Custom JavaScript:
Then the code could be along this lines (grabbed from here), depending on the format you want to return:
function() {
try {
var timestamp = new Date();
var time = timestamp.toString().split(' ');
return time[3]+ " " +time[1]+ " " +time[2]+ " "+time[4];
} catch(e) {
return "unknown";
}
}
After that, a variable with the name given (in this case I used 'Timestamp') will become available as {{Timestamp}} You then can plug it in the event or whatever tag you are creating. You may even use it as {{Timestamp}} inside a Javascript tag. The variable will return a timestamp in the format: 2020 May 30 11:41:44

Fullcalendar - Remove day suffix in dayGridMonth View when using Japanese

When setting locale to Japanese in Fullcalendar and use dayGridMonth View, for each day cell suffix "日"(means day) is added.
I want to remove this day suffix letter, so that the appearance of the cell would be just numbers.
I looked through the document but I couldn't find it.
This behavior can be checked in official demo by setting locale to "ja".
https://fullcalendar.io/docs/locale-demo
I want my day cells to show only numbers, without suffix.
I have read the source code of FullCalendar. It uses Intl.DateTimeFormat to convert date format as below.
https://github.com/fullcalendar/fullcalendar/blob/f86b5f33148cb43178e21c46a6ea814d128a88e1/src/core/datelib/formatting-native.ts#L165
I couldn't find how to customize the format. A possible dirty hack is to fork the repository and remove "日" character after following code.
https://github.com/fullcalendar/fullcalendar/blob/e73621e5c9bcf6778c07cc5a29a4b0641d3ac46e/src/daygrid/DayGrid.ts#L390-L395
html = html.replace('日', '');
There's a option called dayCellDidMount in FullCalendar, which could be called for do something after dayCell has been added.
https://fullcalendar.io/docs/day-cell-render-hooks
So you could replace the original dayText with your custom Text when call this.
code for example:
var calendar = new FullCalendar.Calendar(calendarEl, {
...,
dayCellDidMount: function(info){
var day = moment(info.date).format('DD') // custom the text for example
// hide the original one
var originElement = info.el.querySelectorAll(".fc-daygrid-day-number")
originElement.forEach(e => e.classList.add("d-none") );
// insert new text
var targetElement = info.el.querySelectorAll(".fc-daygrid-day-top");
targetElement.forEach(e => e.innerHTML = day );
},
})
and then you can customize css .fc-daygrid-day-top for the view.

How to export a table as google sheet in Google app maker using a button

I've looked extensively and tried to modify multiple sample sets of codes found on different posts in Stack Overflow as well as template documents in Google App Maker, but cannot for the life of me get an export and en email function to work.
UserRecords table:
This is the area where the data is collected and reviewed, the populated table:
These are the data fields I am working with:
This is what the exported Sheet looks like when I go through the motions and do an export through the Deployment tab:
Lastly, this is the email page that I've built based on tutorials and examples I've seen:
What I've learned so far (based on the circles I'm going round in):
Emails seem mostly straight forward, but I don't need to send a message, just an attachment with a subject, similar to using the code:
function sendEmail_(to, subject, body) {
var emailObj = {
to: to,
subject: subject,
htmlBody: body,
noReply: true
};
MailApp.sendEmail(emailObj);
}
Not sure how to change the "body" to the exported document
To straight up export and view the Sheet from a button click, the closest I've found to a solution is in Document Sample but the references in the code speak to components on the page only. I'm not sure how to modify this to use the table, and also what to change to get it as a sheet instead of a doc.
This may seem trivial to some but I'm a beginner and am struggling to wrap my head around what I'm doing wrong. I've been looking at this for nearly a week. Any help will be greatly appreciated.
In it's simplest form you can do a Google sheet export with the following server script (this is based on a model called employees):
function exportEmployeeTable() {
//if only certain roles or individuals can perform this action include proper validation here
var query = app.models.Employees.newQuery();
var results = query.run();
var fields = app.metadata.models.Employees.fields;
var data = [];
var header = [];
for (var i in fields) {
header.push(fields[i].displayName);
}
data.push(header);
for (var j in results) {
var rows = [];
for (var k in fields) {
rows.push(results[j][fields[k].name]);
}
data.push(rows);
}
if (data.length > 1) {
var ss = SpreadsheetApp.create('Employee Export');
var sheet = ss.getActiveSheet();
sheet.getRange(1,1,data.length,header.length).setValues(data);
//here you could return the URL for your spreadsheet back to your client by setting up a successhandler and failure handler
return ss.getUrl();
} else {
throw new app.ManagedError('No Data to export!');
}
}

Google Form make POST request on submission

Is there a way to call an external API Endpoint on Google Forms every time the form is filled out?
First:
you'll need to set up your App script project and you'll do that by:
Visit script.google.com to open the script editor. (You'll need to be signed in to your Google account.) If this is the first time you've been to script.google.com, you'll be redirected to a page that introduces Apps Script. Click Start Scripting to proceed to the script editor.
A welcome screen will ask what kind of script you want to create. Click Blank Project or Close.
Delete any code in the script editor and paste in the code below.
This video and the doc will help
Second
you'll need to create an installable trigger, you can add it to the form directly or to the spreadsheet that has the responses
function setUpTrigger(){
ScriptApp.newTrigger('sendPostRequest') /* this has the name of the function that will have the post request */
.forForm('formkey') // you'll find it in the url
.onFormSubmit()
.create();
}
Check the doc
Third
create the sendPostRequest function and add the UrlFetchApp to it
function sendPostRequest(e){
// Make a POST request with form data.
var resumeBlob = Utilities.newBlob('Hire me!', 'text/plain', 'resume.txt');
var formData = {
'name': 'Bob Smith',
'email': 'bob#example.com',
'resume': resumeBlob
};
// Because payload is a JavaScript object, it is interpreted as
// as form data. (No need to specify contentType; it automatically
// defaults to either 'application/x-www-form-urlencoded'
// or 'multipart/form-data')
var options = {
'method' : 'post',
'payload' : formData
};
UrlFetchApp.fetch('https://httpbin.org/post', options);
}
Check the doc
Try something like this in your app script:
var POST_URL = "enter your webhook URL";
function onSubmit(e) {
var form = FormApp.getActiveForm();
var allResponses = form.getResponses();
var latestResponse = allResponses[allResponses.length - 1];
var response = latestResponse.getItemResponses();
var payload = {};
for (var i = 0; i < response.length; i++) {
var question = response[i].getItem().getTitle();
var answer = response[i].getResponse();
payload[question] = answer;
}
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
UrlFetchApp.fetch(POST_URL, options);
};
Be sure to replace the POST_URL variable with your webhook, you can use requestcatcher.com to test this out.
Add a trigger to the script by clicking "Triggers" in the side menu
Open the menu (top-right dots)
Click in Script Editor
Paste the above code (changing the POST_URL)
Click in the clock icon (left-side menu), which means Triggers.
On the right-bottom corner, click in the blue Add trigger button (a form will show as the image below).
It should show onSubmit under Choose which function to run.
Make sure Select event type is set as On form submit.
Click Save button.
After that, submit your form and watch for the request to come in.
This is pretty straightforward with Google Scripts.
Just create a new project bound to your spreadsheet and create 2 elements:
A function that will contain all relevant data to make the call (see docs for making a HTTP request from Google Apps Script)
A trigger linked to the spreadsheet. You can set it to run each time an edit occurs or form is submitted
Voilà, your sheet will call whatever endpoint you wish on submission. You can even parse the spreadsheet to return that data to your endpoint

Set email body content in google appmaker

I want to send an email with a content related to my data such as in following piece of code I found on Datasource script of Google AppMaker Project Tracker template. But I don't understand how it works. How that data.modifiedBy reflect to the record in my datasource?
Any help from the floors? Thanks ..
Look at the Notifications server side script in the template.
It has method notifyAboutItemChanges_ which is passing the data to this record.
function notifyAboutItemChanges_(changes) {
var settings = getAppSettingsRecord_()[0];
if (!settings.EnableEmailNotifications) {
return;
}
var data = {
appUrl: settings.AppUrl,
itemType: changes[0].Type,
itemKey: changes[0]._key,
itemName: changes[0].Name,
modifiedBy: changes[0].ModifiedBy,
changes: changes
};
// Email subject.
var subjectTemplate =
HtmlService.createTemplate(settings.NotificationEmailSubject);
}
This function is passing this data to your settings record.
So no magic here :) You need to pass the data to your record which will be replaced at run time with the values.
For more details on Email refer this sample app.

Resources