質問編集履歴

3

fd

2019/09/27 01:20

投稿

kotaro_nagano
kotaro_nagano

スコア11

test CHANGED
File without changes
test CHANGED
@@ -88,8 +88,12 @@
88
88
 
89
89
  86: }
90
90
 
91
+
92
+
91
93
  ・・・中略
92
94
 
95
+
96
+
93
97
  function setAuthAndDependencies(auth) {
94
98
 
95
99
  google_auth = auth;
@@ -108,6 +112,308 @@
108
112
 
109
113
  }
110
114
 
115
+
116
+
117
+ ・・・中略
118
+
119
+
120
+
121
+ // public API methods
122
+
123
+ this.getInfo = function(cb) {
124
+
125
+ self.makeFeedRequest(["worksheets", ss_key], "GET", null, function(
126
+
127
+ err,
128
+
129
+ data
130
+
131
+ ) {
132
+
133
+ if (err) return cb(err);
134
+
135
+ if (data === true) {
136
+
137
+ return cb(new Error("No response to getInfo call"));
138
+
139
+ }
140
+
141
+ var ss_data = {
142
+
143
+ id: data.id,
144
+
145
+ title: data.title,
146
+
147
+ updated: data.updated,
148
+
149
+ author: data.author,
150
+
151
+ worksheets: []
152
+
153
+ };
154
+
155
+ var worksheets = forceArray(data.entry);
156
+
157
+ worksheets.forEach(function(ws_data) {
158
+
159
+ ss_data.worksheets.push(new SpreadsheetWorksheet(self, ws_data));
160
+
161
+ });
162
+
163
+ self.info = ss_data;
164
+
165
+ self.worksheets = ss_data.worksheets;
166
+
167
+ cb(null, ss_data);
168
+
169
+ });
170
+
171
+ };
172
+
173
+
174
+
175
+ ・・・中略
176
+
177
+
178
+
179
+ // This method is used internally to make all requests
180
+
181
+ this.makeFeedRequest = function(url_params, method, query_or_data, cb) {
182
+
183
+ var url;
184
+
185
+ var headers = {};
186
+
187
+ if (!cb) cb = function() {};
188
+
189
+ if (typeof url_params == "string") {
190
+
191
+ // used for edit / delete requests
192
+
193
+ url = url_params;
194
+
195
+ } else if (Array.isArray(url_params)) {
196
+
197
+ //used for get and post requets
198
+
199
+ url_params.push(visibility, projection);
200
+
201
+ url = GOOGLE_FEED_URL + url_params.join("/");
202
+
203
+ }
204
+
205
+
206
+
207
+ async.series({
208
+
209
+ auth: function(step) {
210
+
211
+ if (auth_mode != "jwt") return step();
212
+
213
+ // check if jwt token is expired
214
+
215
+ if (google_auth && google_auth.expires > +new Date()) return step();
216
+
217
+ renewJwtAuth(step);
218
+
219
+ },
220
+
221
+ request: function(result, step) {
222
+
223
+ if (google_auth) {
224
+
225
+ if (google_auth.type === "Bearer") {
226
+
227
+ headers["Authorization"] = "Bearer " + google_auth.value;
228
+
229
+ } else {
230
+
231
+ headers["Authorization"] = "GoogleLogin auth=" + google_auth;
232
+
233
+ }
234
+
235
+ }
236
+
237
+
238
+
239
+ headers["Gdata-Version"] = "3.0";
240
+
241
+
242
+
243
+ if (method == "POST" || method == "PUT") {
244
+
245
+ headers["content-type"] = "application/atom+xml";
246
+
247
+ }
248
+
249
+
250
+
251
+ if (
252
+
253
+ method == "PUT" ||
254
+
255
+ (method == "POST" && url.indexOf("/batch") != -1)
256
+
257
+ ) {
258
+
259
+ headers["If-Match"] = "*";
260
+
261
+ }
262
+
263
+
264
+
265
+ if (method == "GET" && query_or_data) {
266
+
267
+ var query = "?" + querystring.stringify(query_or_data);
268
+
269
+ // replacements are needed for using structured queries on getRows
270
+
271
+ query = query.replace(/%3E/g, ">");
272
+
273
+ query = query.replace(/%3D/g, "=");
274
+
275
+ query = query.replace(/%3C/g, "<");
276
+
277
+ url += query;
278
+
279
+ }
280
+
281
+
282
+
283
+ request(
284
+
285
+ {
286
+
287
+ url: url,
288
+
289
+ method: method,
290
+
291
+ headers: headers,
292
+
293
+ gzip: options.gzip !== undefined ? options.gzip : true,
294
+
295
+ body: method == "POST" || method == "PUT" ? query_or_data : null
296
+
297
+ },
298
+
299
+ function(err, response, body) {
300
+
301
+ if (err) {
302
+
303
+ return cb(err);
304
+
305
+ } else if (response.statusCode === 401) {
306
+
307
+ return cb(new Error("Invalid authorization key."));
308
+
309
+ } else if (response.statusCode >= 400) {
310
+
311
+ var message = _.isObject(body)
312
+
313
+ ? JSON.stringify(body)
314
+
315
+ : body.replace(/&quot;/g, '"');
316
+
317
+ return cb(
318
+
319
+ new Error(
320
+
321
+ "HTTP error " +
322
+
323
+ response.statusCode +
324
+
325
+ " (" +
326
+
327
+ http.STATUS_CODES[response.statusCode]
328
+
329
+ ) +
330
+
331
+ ") - " +
332
+
333
+ message
334
+
335
+ );
336
+
337
+ } else if (
338
+
339
+ response.statusCode === 200 &&
340
+
341
+ response.headers["content-type"].indexOf("text/html") >= 0
342
+
343
+ ) {
344
+
345
+ return cb(
346
+
347
+ new Error(
348
+
349
+ "Sheet is private. Use authentication or make public. (see https://github.com/theoephraim/node-google-spreadsheet#a-note-on-authentication for details)"
350
+
351
+ )
352
+
353
+ );
354
+
355
+ }
356
+
357
+
358
+
359
+ if (body) {
360
+
361
+ var xml_parser = new xml2js.Parser({
362
+
363
+ // options carried over from older version of xml2js
364
+
365
+ // might want to update how the code works, but for now this is fine
366
+
367
+ explicitArray: false,
368
+
369
+ explicitRoot: false
370
+
371
+ });
372
+
373
+ xml_parser.parseString(body, function(err, result) {
374
+
375
+ if (err) {
376
+
377
+ xml_parser = null;
378
+
379
+ body = null;
380
+
381
+ return cb(err);
382
+
383
+ }
384
+
385
+ if (cb.length == 3) {
386
+
387
+ cb(null, result, body);
388
+
389
+ } else {
390
+
391
+ body = null;
392
+
393
+ cb(null, result);
394
+
395
+ }
396
+
397
+ });
398
+
399
+ } else {
400
+
401
+ if (err) cb(err);
402
+
403
+ else cb(null, true);
404
+
405
+ }
406
+
407
+ }
408
+
409
+ );
410
+
411
+ }
412
+
413
+ });
414
+
415
+ };
416
+
111
417
  ```
112
418
 
113
419
 

2

f

2019/09/27 01:20

投稿

kotaro_nagano
kotaro_nagano

スコア11

test CHANGED
File without changes
test CHANGED
@@ -50,72 +50,194 @@
50
50
 
51
51
 
52
52
 
53
+ this.setAuthToken = function(auth_id) {
54
+
55
+ if (auth_mode == "anonymous") auth_mode = "token";
56
+
57
+ setAuthAndDependencies(auth_id);
58
+
59
+ };
60
+
61
+
62
+
63
+ ...中略
64
+
65
+
66
+
53
- function renewJwtAuth(cb) {
67
+ 75: function renewJwtAuth(cb) {
54
-
68
+
55
- auth_mode = "jwt";
69
+ 76: auth_mode = "jwt";
56
-
70
+
57
- jwt_client.authorize(function(err, token) {
71
+ 77: jwt_client.authorize(function(err, token) {
58
-
72
+
59
- if (err) return cb(err);
73
+ 78: if (err) return cb(err);
60
-
74
+
61
- self.setAuthToken({
75
+ 79: self.setAuthToken({
62
-
76
+
63
- type: token.token_type,
77
+ 80: type: token.token_type,
64
-
78
+
65
- value: token.access_token,
79
+ 81: value: token.access_token,
66
-
80
+
67
- expires: token.expiry_date
81
+ 82: expires: token.expiry_date
82
+
68
-
83
+ 83: });
84
+
85
+ 84: cb();
86
+
87
+ 85: });
88
+
89
+ 86: }
90
+
91
+ ・・・中略
92
+
93
+ function setAuthAndDependencies(auth) {
94
+
95
+ google_auth = auth;
96
+
97
+ if (!options.visibility) {
98
+
99
+ visibility = google_auth ? "private" : "public";
100
+
101
+ }
102
+
103
+ if (!options.projection) {
104
+
105
+ projection = google_auth ? "full" : "values";
106
+
107
+ }
108
+
109
+ }
110
+
111
+ ```
112
+
113
+
114
+
115
+ ### 該当のソースコード
116
+
117
+
118
+
119
+ ```js
120
+
121
+ "use strict";
122
+
123
+
124
+
125
+ const googleSpreadSheet = require("google-spreadsheet");
126
+
127
+
128
+
129
+ const doc = new googleSpreadSheet(
130
+
131
+ "1X98eannyhE"
132
+
133
+ );
134
+
135
+
136
+
137
+ let sheet;
138
+
139
+
140
+
141
+ const setAuth = () =>
142
+
143
+ new Promise((resolve, reject) => {
144
+
145
+ const creds = require("./903c28f.json");
146
+
147
+ doc.useServiceAccountAuth(creds, getInfoAndSheets());
148
+
69
- });
149
+ });
150
+
151
+
152
+
70
-
153
+ const getInfoAndSheets = () =>
154
+
155
+ new Promise((resolve, reject) => {
156
+
71
- cb();
157
+ doc.getInfo((err, info) => {
158
+
159
+ sheet = info.worksheets[0];
72
160
 
73
161
  });
74
162
 
163
+ resolve();
164
+
165
+ });
166
+
167
+
168
+
169
+ const workingWithCells = () => {
170
+
171
+ const COLUMS = {
172
+
173
+ name: 1,
174
+
175
+ price: 2
176
+
177
+ };
178
+
179
+ sheet.getCells(
180
+
181
+ {
182
+
183
+ "min-row": 2,
184
+
185
+ "max-row": 5,
186
+
187
+ "return-empty": true
188
+
189
+ },
190
+
191
+ (err, cells) => {
192
+
193
+ for (let i = 0; i < cells.length / sheet.colCount; i += 1) {
194
+
195
+ const name = cells[i * sheet.colCount + COLUMS.name].value;
196
+
197
+ const price = cells[i * sheet.colCount + COLUMS.price].value;
198
+
75
- }
199
+ }
200
+
76
-
201
+ }
202
+
203
+ );
204
+
205
+ };
206
+
207
+
208
+
209
+ const a = async () => {
210
+
211
+ await setAuth();
212
+
213
+ await workingWithCells();
214
+
215
+ };
216
+
217
+
218
+
219
+ const main = () => {
220
+
221
+ a();
222
+
223
+ };
224
+
225
+
226
+
227
+ main();
228
+
229
+
230
+
77
- ```
231
+ ```
78
-
79
-
80
-
232
+
233
+
234
+
81
- ### 該当のソースコード
235
+ ### 試したこと
82
236
 
83
237
 
84
238
 
85
239
  ```js
86
240
 
87
- "use strict";
88
-
89
-
90
-
91
- const googleSpreadSheet = require("google-spreadsheet");
92
-
93
-
94
-
95
- const doc = new googleSpreadSheet(
96
-
97
- "1X98eannyhE"
98
-
99
- );
100
-
101
-
102
-
103
- let sheet;
104
-
105
-
106
-
107
- const setAuth = () =>
108
-
109
- new Promise((resolve, reject) => {
110
-
111
- const creds = require("./903c28f.json");
112
-
113
- doc.useServiceAccountAuth(creds, getInfoAndSheets());
114
-
115
- });
116
-
117
-
118
-
119
241
  const getInfoAndSheets = () =>
120
242
 
121
243
  new Promise((resolve, reject) => {
@@ -130,79 +252,13 @@
130
252
 
131
253
  });
132
254
 
133
-
134
-
135
- const workingWithCells = () => {
136
-
137
- const COLUMS = {
138
-
139
- name: 1,
140
-
141
- price: 2
142
-
143
- };
144
-
145
- sheet.getCells(
146
-
147
- {
148
-
149
- "min-row": 2,
150
-
151
- "max-row": 5,
152
-
153
- "return-empty": true
154
-
155
- },
156
-
157
- (err, cells) => {
158
-
159
- for (let i = 0; i < cells.length / sheet.colCount; i += 1) {
160
-
161
- const name = cells[i * sheet.colCount + COLUMS.name].value;
162
-
163
- const price = cells[i * sheet.colCount + COLUMS.price].value;
164
-
165
- }
166
-
167
- }
168
-
169
- );
170
-
171
- };
172
-
173
-
174
-
175
- const a = async () => {
176
-
177
- await setAuth();
178
-
179
- await workingWithCells();
180
-
181
- };
182
-
183
-
184
-
185
- const main = () => {
186
-
187
- a();
188
-
189
- };
190
-
191
-
192
-
193
- main();
194
-
195
-
196
-
197
- ```
255
+ ```
198
-
199
-
200
-
256
+
201
- ### 試したこと
257
+ 上記の関数を下記に変更
202
-
203
-
204
-
258
+
259
+
260
+
205
- ```js
261
+ ```
206
262
 
207
263
  const getInfoAndSheets = () =>
208
264
 
@@ -210,6 +266,8 @@
210
266
 
211
267
  doc.getInfo((err, info) => {
212
268
 
269
+ if(!info) info = function(){}
270
+
213
271
  sheet = info.worksheets[0];
214
272
 
215
273
  });
@@ -218,30 +276,6 @@
218
276
 
219
277
  });
220
278
 
221
- ```
222
-
223
- 上記の関数を下記に変更
224
-
225
-
226
-
227
- ```
228
-
229
- const getInfoAndSheets = () =>
230
-
231
- new Promise((resolve, reject) => {
232
-
233
- doc.getInfo((err, info) => {
234
-
235
- if(!info) info = function(){}
236
-
237
- sheet = info.worksheets[0];
238
-
239
- });
240
-
241
- resolve();
242
-
243
- });
244
-
245
279
 
246
280
 
247
281
 

1

s

2019/09/27 01:17

投稿

kotaro_nagano
kotaro_nagano

スコア11

test CHANGED
File without changes
test CHANGED
@@ -44,6 +44,40 @@
44
44
 
45
45
 
46
46
 
47
+ ```js
48
+
49
+ /Users/user/dev/atom-tools/node_modules/google-spreadsheet/index.js
50
+
51
+
52
+
53
+ function renewJwtAuth(cb) {
54
+
55
+ auth_mode = "jwt";
56
+
57
+ jwt_client.authorize(function(err, token) {
58
+
59
+ if (err) return cb(err);
60
+
61
+ self.setAuthToken({
62
+
63
+ type: token.token_type,
64
+
65
+ value: token.access_token,
66
+
67
+ expires: token.expiry_date
68
+
69
+ });
70
+
71
+ cb();
72
+
73
+ });
74
+
75
+ }
76
+
77
+ ```
78
+
79
+
80
+
47
81
  ### 該当のソースコード
48
82
 
49
83