-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCode.gs
99 lines (84 loc) · 2.47 KB
/
Code.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
97
98
99
/**
* Passes inputs to https://fuzzy.ai
*
* @param {string} input The Fuzzy.ai Agent input name
* @param {number} value The value to pass.
* @customfunction
*/
function FUZZYAI(input) {
var inputs = {};
var docProperties = PropertiesService.getDocumentProperties();
var apiKey = docProperties.getProperty('FUZZYAI_API_KEY');
var agentID = docProperties.getProperty('FUZZYAI_AGENT_ID');
if (!apiKey || apiKey == "" || !agentID || agentID == "") {
return "Settings required. See Fuzzy.ai > Settings";
}
for (var i = 0; i < arguments.length; i += 2) {
inputs[arguments[i]] = arguments[i+1];
}
if (inputs["version"]) {
delete inputs["version"];
}
var payload = JSON.stringify(inputs);
var headers = {
'Authorization': 'Bearer ' + apiKey,
'Content-type': 'application/json'
};
var url = 'https://api.fuzzy.ai/agent/' + agentID;
var options = {
'method': 'post',
'headers': headers,
'payload': payload
};
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() != 200) {
throw new Error("Error using the fuzzy.ai API");
}
var outputs = JSON.parse(response.getContentText("UTF-8"));
var results = [[]];
for (var output in outputs) {
results[0].push(outputs[output]);
}
return results;
}
/**
* Deprecated use FUZZYAI instead.
*
* @customfunction
*/
function FUZZIFY(input) {
return FUZZYAI.apply(this, arguments);
}
// onInstall function
function onInstall(e) {
onOpen(e);
}
// Add custom menu option
function onOpen(e) {
SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Settings', 'showSettingsDialog')
.addToUi();
}
// Loads Settings.html as a template and populates existing values
function getSettings() {
var docProperties = PropertiesService.getDocumentProperties();
t = HtmlService.createTemplateFromFile('Settings');
t.apiKey = docProperties.getProperty('FUZZYAI_API_KEY');
t.agentID = docProperties.getProperty('FUZZYAI_AGENT_ID');
return t.evaluate();
}
// Show settings dialog from template above.
function showSettingsDialog() {
var html = getSettings();
html.setWidth(400);
html.setHeight(300);
SpreadsheetApp.getUi()
.showModalDialog(html, 'Fuzzy.ai Settings');
}
// Save handler for settings form.
function saveSettings(formObject) {
var docProperties = PropertiesService.getDocumentProperties();
docProperties.setProperty('FUZZYAI_API_KEY', formObject.apiKey);
docProperties.setProperty('FUZZYAI_AGENT_ID', formObject.agentID);
}