-
Notifications
You must be signed in to change notification settings - Fork 4
/
postToFilemaker.gs
96 lines (85 loc) · 3.17 KB
/
postToFilemaker.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function postToFilemaker(e) {
//--- parse the "e" event object that googlesheets passes the script ---//
validate()
var googleFormData = JSON.stringify(e) ; // Logger.log(googleFormData);
//--- configure filemaker connection ---//
var serverPath = "https://fm107.beezwax.net/fmi/rest/api" ;
var filename = "google_forms_rest";
var account = "google_form" ;
var password = "<password>" ; // use your own password here
//--- configure filemaker data ---//
var layout = "incoming_google_forms" ;
var recordData = { "form_data":googleFormData ,
"name":"Show and Share",
"type":"Example Form"
};
//--- all configurations should be set above this comment
//--- there should be no need to edit the code below...
login()
post()
logout()
//-- functions
function login() {
//authenticate to get FM-Data-token using http
authUrl = serverPath.concat( "/auth/" , filename ) ;
var data = { "layout":layout,
"user":account,
"password":password
}
var payload = JSON.stringify(data);
var headers =
{
"content-type": "application/json",
};
var options =
{
"method": "POST",
"headers": headers,
"payload": payload
};
var response = UrlFetchApp.fetch(authUrl,options); // Logger.log(response.getContentText());
var dataAll = JSON.parse(response.getContentText());
fmDataToken = dataAll.token;
}
function post() {
// use fmDataToken to post to Filamaker using http
var url = serverPath.concat("/record/",filename,"/",layout); // Logger.log(url); // var url = "https://fm107.beezwax.net/fmi/rest/api/record/epatt_rest/rest_data";
var headers =
{
"content-type": "application/json",
"FM-Data-token": fmDataToken
};
var data = { "data":recordData } ;
var payload = JSON.stringify(data); // Logger.log(payload);
var options =
{
"method": "POST",
"headers": headers,
"payload" : payload
}
var response = UrlFetchApp.fetch(url,options);
var fmApiResponse = response ;
}
function logout() {
// logout fmDataToken using http
var headers =
{
"content-type": "application/json",
"FM-Data-token": fmDataToken
};
var options =
{
"method": "DELETE",
"headers": headers,
};
var response = UrlFetchApp.fetch(authUrl,options); // Logger.log(response);
}
function validate() {
// e is the "event" parameter passed from Google Forms.
// if e is empty, we are debugging script and running it from script editor where no event parameter would be passed
if (!e){
e = {"values":["Testing"],"namedValues":{"Message":["Test Data While Debugging"],"Timestamp":["2/9/2018 20:17:37"]},"range":{},"source":{},"authMode":{},"triggerUid":"1296973926"}
}
googleFormNamedValues = JSON.stringify(e.namedValues) ; // Logger.log(googleFormData);
}
}