forked from sammachin/node-red-contrib-google-sheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsheet.js
executable file
·152 lines (147 loc) · 5.04 KB
/
gsheet.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
let {google} = require('googleapis');
module.exports = function(RED) {
function gSheet(config) {
RED.nodes.createNode(this, config);
var node = this;
this.creds = JSON.parse(RED.nodes.getNode(config.creds).credentials.creds);
this.method = config.method;
this.flatten = config.flatten
node.on('input', function(msg) {
if(!config.sheet){
this.sheet = msg.sheet
} else {
this.sheet = config.sheet;
}
if(!config.cells){
this.cells = msg.topic
} else {
this.cells = config.cells;
}
let jwtClient = new google.auth.JWT(
this.creds.client_email,
null,
this.creds.private_key,
['https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive']
);
let sheet = this.sheet;
let cells = this.cells;
let method = this.method;
let flaten = this.flatten;
let sheets = google.sheets('v4');
jwtClient.authorize(function (err, tokens) {
if (err) {
node.error("Auth Error: " +err);
return;
} else {
switch(method) {
case 'get':
sheets.spreadsheets.values.get({
auth: jwtClient,
spreadsheetId: sheet,
range: cells
}, function (err, response) {
if (err) {
node.error('The API returned an error: ' + err, msg);
} else {
if (flaten){
msg.payload = response.data.values.flat();
} else {
msg.payload = response.data.values;
}
node.send(msg);
}
});
break;
case 'append':
if (Array.isArray(msg.payload)){
if (Array.isArray(msg.payload[0])){
msg.payload = {"values": msg.payload}
} else {
msg.payload = {"values": [msg.payload]}
}
} else{
msg.payload = {"values": [[msg.payload]]}
}
sheets.spreadsheets.values.append({
auth: jwtClient,
spreadsheetId: sheet,
range: cells,
valueInputOption: "USER_ENTERED",
insertDataOption: "INSERT_ROWS",
resource: msg.payload
}, function (err, response) {
if (err) {
node.error('The API returned an error: ' + err, msg);
} else {
msg.payload = response.data.updates;
node.send(msg);
}
});
break;
case 'update':
if (Array.isArray(msg.payload)){
if (Array.isArray(msg.payload[0])){
msg.payload = {"values": msg.payload}
} else {
msg.payload = {"values": [msg.payload]}
}
} else{
msg.payload = {"values": [[msg.payload]]}
}
sheets.spreadsheets.values.update({
auth: jwtClient,
spreadsheetId: sheet,
range: cells,
valueInputOption: "USER_ENTERED",
resource: msg.payload
}, function (err, response) {
if (err) {
node.error('The API returned an error: ' + err, msg);
} else {
msg.payload = response.data
node.send(msg);
}
});
break;
case 'clear':
sheets.spreadsheets.values.clear({
auth: jwtClient,
spreadsheetId: sheet,
range: cells,
}, function (err, response) {
if (err) {
node.error('The API returned an error: ' + err, msg);
} else {
msg.payload = response.data;
node.send(msg);
}
});
break;
}
}
});
});
}
function gauth(n){
RED.nodes.createNode(this, n);
this.creds = n.creds;
}
RED.nodes.registerType("GSheet",gSheet);
RED.nodes.registerType("gauth",gauth,{
credentials: {
creds: {type:"text"}
}
});
}
function flatten(arr) {
if (arr.length == 1 && arr[0].length == 1){
return arr[0][0];
} else if (arr.length != 1 && arr[0].length == 1){
return arr.reduce((acc, val) => acc.concat(val), []);
} else if (arr.length == 1 && arr[0].length != 1){
return arr.reduce((acc, val) => acc.concat(val), []);
} else{
return arr
}
}