Need some help assigning answer to google app maker form - google-app-maker

on a google app maker form, How do you assign an answer to a question based on another answer or if the question is blank.
for example the time entry form question asks for the date of the entry, if it is blank how do i make the answer todays date?

Based on your use case I would suggest the following:
Datebox widget>events>onAttach event enter the following code:
var today = new Date();
widget.value = new Date(today.getFullYear(), today.getMonth(), today.getDate());
Then enter the following in the onClick event of your 'Submit button', this will repopulate the datebox widget after a new entry was successfully submitted:
widget.datasource.createItem(function() {
var today = new Date();
widget.datasource.item.Date = new Date(today.getFullYear(), today.getMonth(), today.getDate());
});

Related

How to change date format in Google Forms Response?

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. :)

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

Google App Maker how to save and update records?

We have methods to create and update the item in App Maker, but it will save the whole record. If I want to update only a particular field on the button click, there is no option.
Please post if there are any options to update a particular field?
Automatic Save Mode
In auto save mode App Maker instantly saves every field, so you have per-field saving granularity. In other words, whenever field's value is changed App Maker sends request to server to save the modification.
// this code will trigger async call to server to save the modification
// only for this particular field.
app.datasources.MyDatasource.item.MyField = 'My new value';
Manual Save Mode
With manual save mode there is no easy way to save modifications subsets separately, since whenever you call 'saveChanges' method App Maker will try to persist all modifications made to the datasource. Here are some pretty bad workarounds that I would not recommend unless you have no other options:
// Implementation with callback chaining if field save
// order matters. It will work extremely slow.
var ds = app.datasources.MyDatasource;
ds.item.MyField1 = 'My new value 1';
ds.saveChanges(function() {
ds.item.MyField2 = 'My new value 2';
ds.saveChanges(function() {
ds.item.MyField3 = 'My new value 3';
...
});
});
// Implementation without chaining. In theory should work
// faster(if it would work at all...)
var ds = app.datasources.MyDatasource;
ds.item.MyField1 = 'My new value 1';
ds.saveChanges();
ds.item.MyField2 = 'My new value 2';
ds.saveChanges();
ds.item.MyField3 = 'My new value 3';
ds.saveChanges();
...
Server Script
Pretty much same answer as for Manual Save mode, it is doable, but I would not recommend do it since performance will significantly degrade.
record.MyField1 = 'My new value 1';
app.saveRecords([record]);
record.MyField2 = 'My new value 2';
app.saveRecords([record]);
record.MyField3 = 'My new value 3';
app.saveRecords([record]);
...

data not set using .set on Firebase

First question here,
I am trying to follow the startup guide on the Firebase, but as I am trying to click the send button, the data is not transferred to the data server.
Here is the code:
var yjDataRef = new Firebase('https://yjyc-signup.firebaseio.com/');
var name = document.getElementById('name');
var email = document.getElementById('email');
var submitBtn = document.getElementById('submit');
nameRef = yjDataRef.child('nameRef');
submitBtn.addEventListener('click' function() {
nameRef.set(name: 'name');
});
I have called all of them but still, the data does not transferred to the data center.
Thank you so much for your help.
I believe you need a comma after 'click'.
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

Updating dojox.grid.DataGrid

I have a dojox.grid.DataGrid configured and I want to populate a data grid with different values when a user clicks a button.
I have tried the following code but it does not work:
var employees2 = [
{name:"Jane", department:"engineering"},
{name:"Mary", department:"engineering"},
{name:"Julie", department:"sales"}
];
console.info("grid is "+grid.toString());
employeeStore2 = new dojo.store.Memory({data:employees2, idProperty: "name"});
grid.setStore(employeeStore2);
employeeStore2.close();
I have setup the example here: http://jsfiddle.net/nonie/kx72T/
Any help would be great.
In your example, the event onclick button doesn't work....
I think in showList2 method, you should write something like this
**employeeStore2 = new dojo.store.Memory({
data: employees2,
idProperty: "name"
});
grid.setStore(dojo.data.ObjectStore({objectStore: employeeStore2}));
grid.update();**

Resources