1

I'm using Google SpreadSheet and IFTTT to do a DB Call Log of my phone, this is working perfect. Now I'm trying to populate a form in a web page by API from this DB Call Log. I would like to send lastRow to my API every time IFTTT populate the Sheet. The first Row in the Sheet is populated with headers name: departament, first_name, last_name, email, phone, deadline.

So i manage to send data to API like this:

function myFunction() {

var data = {

    'department':     1,
    'first_name' :    'Test',
    'last_name' :     'test',
    'email' :         'email@gmail.com',
    'phone' :         ["0700000000"],
    'deadline' :      '2017-04-10T00:00'
}

var payload = JSON.stringify(data)  

var headers = {

     'AUTHORIZATION': 'Token b8473654v6345teryrby456yrtyrtyertytdvfh87afc',
     // Add any other required parameters for XXX API.
};
var url = 'http://api.XXX.com/api/1.0/clients/';
var options = {
    'method': 'post',
    'contentType': 'application/json', 
    'headers': headers,
    'payload' : payload,
};
var response = UrlFetchApp.fetch(url, options);
}

Now i need to automate it but i don't know how:

  1. (this is a SpreadSheet question) IFTTT populate the "deadline" column in this format "April 10, 2017 at 01:54PM" however the needed format for API is "2017-04-10T13:54", how to auto modify it?

  2. to get the values from Sheet cells (from lastRow) and send them throw json payload

  3. to set a trigger event so the script trigger's every time IFTTT populates a new Row in the Sheet.

Thank you!

1 Answers1

2

Will try to answer the question one by one:

1) Reformat date: You can use Utilities.formatDate() in apps script to modify your date.

Code:

function reformatDate(dtStr)
{
 if (dtStr == undefined)
 dtStr = "April 1, 2017 at 01:54PM"

 dtStr = dtStr.replace("at", "") // Remove at
 dtStr = dtStr.replace(/(\d)(PM)/g,"$1 $2") //Add a space between the time and PM
 dtStr = dtStr.replace(/(\d)(AM)/g,"$1 $2") //Add a space between the time and AM
 Logger.log(dtStr)

 dtStr = new Date(dtStr)
 var newDt = Utilities.formatDate(dtStr, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "yyyy-MM-dd'T'HH:mm") 
 Logger.log(newDt)

 return newDt
}

2) Get last row Values: You can use getLastRow() and getValues() functions in apps scripts

function lastRowData(){
 var ss = SpreadsheetApp.getActive()
 var sheet = ss.getActiveSheet()
 var lastRow = sheet.getLastRow() 
 var lastCol = sheet.getLastColumn()
 var lastRowData = sheet.getRange(lastRow,1,1,lastCol).getValues()
 return lastRowData[0]

}

Edit

To get values as is i.e. displayed values in the sheet, you can modify the getvalues to getDisplayValues() like so:

var lastRowData = sheet.getRange(lastRow,1,1,lastCol).getDisplayValues()

3) Trigger your sheet: I will not reinvent the wheel here. But will provide you with an awesome answer from @Mogsdad Link: Trigger an email when a cell is written into from another app (IFTTT)

In short, you will have to use onEdit Trigger to detect new entries.
If on edit doesn't work, try on Change trigger.

Complete code:

function myFunction() {
var lastRow = lastRowData()
var data = {

    'department':     lastRow[0],
    'first_name' :    lastRow[1],
    'last_name' :     lastRow[2],
    'email' :         lastRow[3]',
    'phone' :         [lastRow[4]],
    'deadline' :      reformatDate(lastRow[5])
}

var payload = JSON.stringify(data)  

var headers = {

     'AUTHORIZATION': 'Token b8473654v6345teryrby456yrtyrtyertytdvfh87afc',
     // Add any other required parameters for XXX API.
};
var url = 'http://api.XXX.com/api/1.0/clients/';
var options = {
    'method': 'post',
    'contentType': 'application/json', 
    'headers': headers,
    'payload' : payload,
};
var response = UrlFetchApp.fetch(url, options);
}

Hope that helps!

Community
  • 1
  • 1
Jack Brown
  • 5,802
  • 2
  • 12
  • 27
  • Department no is 1.0 i needed 1 (no decimals). Phone no is not correct i need a 10 digits no, my current no is 727656656 and i need 0727656656. [code]function createSpreadsheetEditTrigger() { var ss = SpreadsheetApp.getActive(); ScriptApp.newTrigger('PipeLine') .forSpreadsheet(ss) .onEdit() .create(); } [code] This is the piece of code for Trigger but is not working as expected( it triggers when I modify a cell in the sheet. but it's not working when a new Row is auto inserted by IFTTT. – Lucian Medisan Apr 12 '17 at 07:58
  • I have edited the above answer to show the display value as is! – Jack Brown Apr 12 '17 at 16:06
  • As for the trigger, could you try onChange event instead and see if that gets triggered by an update from IFTTT. – Jack Brown Apr 12 '17 at 17:04
  • It's working with onChange. Here is the code for the trigger [code]function onChange(e) { var ss = SpreadsheetApp.getActive(); ScriptApp.newTrigger('PipeLine') .forSpreadsheet(ss) .onChange() .create(); }[code] But getDisplayValues() is not doing anything. maybe because the text come in this format from IFTTT, The phone no. it's without 0 so i have to use a ".replace" command to modify it. I tried with custom number format, it's visually working (i can see the change in sheet) but the script doesn't get the right format. Thank you again!!! – Lucian Medisan Apr 12 '17 at 22:49
  • Jack Brown . I notice a problem with function dataReformat. The time is wrong, exactly the minutes. For example "April 1, 2017 at 01:54PM" it's reformated in "2017-04-01T13:05". It cuts the last no. from minute format (in this example "4"). The problem is persistent with any combination of numbers or PM/AM format. Tx. – Lucian Medisan Apr 18 '17 at 12:14
  • Run the reformatedate function by itself and look at the logs. Let me know what's the output. – Jack Brown Apr 18 '17 at 13:09
  • That is exactly what i did. I think the problem lies in dtStr = dtStr.replace(/\d(PM)/g, " PM") and dtStr = dtStr.replace(/\d(AM)/, " AM") – Lucian Medisan Apr 18 '17 at 13:38
  • Thank you very much!!! I have another question but it's regarding next step in my little call log. So i'l post a new question. Tx again for all your help. – Lucian Medisan Apr 18 '17 at 21:46