Skip to content

Commit

Permalink
finished google-spreadsheet adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
pantsel committed Nov 1, 2016
1 parent 18d1b2a commit b4960c7
Showing 1 changed file with 94 additions and 42 deletions.
136 changes: 94 additions & 42 deletions backend/api/services/remote/adapters/google-spreadsheet.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
var fs = require('fs');
var google = require('googleapis');
var path = require('path')
var mkdirp = require('mkdirp');

module.exports = {
schema : {
"name": "Google Spreadsheet",
"value": "google-spreadsheet",
"description": "Import Consumers from Google spreadsheets",
"hasFiles" : true,
"description": "Import Consumers from Google spreadsheets. ",
"info" : "Konga uses the JWT method to authenticate with Google spreadsheets API." +
"Make sure you have created <a href='https://developers.google.com/identity/protocols/application-default-credentials'>Service account JSON credentials</a> for Konga " +
"and that your spreadsheet is shared with that service account's email.",
"form_fields": {
"connection": {
"file" : {
"name" : "JSON credentials file",
"type" : "file",
"description" : "The Service account JSON credentials file." +
"This file only needs to be uploaded once as long as the service account remains as is."
},
"spreadsheetId": {
"name": "spreadsheetId",
"type": "text",
Expand All @@ -20,69 +32,109 @@ module.exports = {
"required" : true,
"description": "The range to read, in A1 notation. ex. A1:B15"
},
"ignore_first_row": {
"name": "Ignore first row",
"type": "boolean",
"description": "Whether or not to ignore the first row of the spreadsheet." +
" This can be useful in case it contains the columns titles."
},
},
"consumer": {
"username": {
"name": "username column",
"type": "number",
"required": true,
"description": "The spreadsheet column that will be used as the consumer <code>username</code>. The first column is 0."
"description": "The index of the spreadsheet column that will be used as the consumer <code>username</code>. The first column is 0."
},
"custom_id": {
"name": "custom_id column",
"type": "number",
"required": true,
"description": "The spreadsheet column that will be used as the consumer <code>custom_id</code>. The first column is 0."
"description": "The index of the spreadsheet column that will be used as the consumer <code>custom_id</code>. The first column is 0."
}
}
}
},
methods : {
loadConsumers : function(req,res) {

var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];

// Load client secrets from a local file.
var key = require('../credentials/google-spreadsheet/konga-84413cd1c5f8.json')

var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES,
null
);

jwtClient.authorize(function (err, tokens) {
if (err) return res.negotiate(err)
listConsumers(jwtClient)
});

function listConsumers(auth) {
var sheets = google.sheets('v4');
sheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: req.body.spreadsheetId,
range : req.body.range,
}, function(err, response) {
req.file('file').upload(function (err, uploadFiles) {
if (err) return res.negotiate(err);

var CREDENTIALS_PATH = path.join(__dirname,"..","credentials/google-spreadsheets");

mkdirp(CREDENTIALS_PATH, function (err) {
if (err) return res.negotiate(err)

//var rows = response.values;
if (response.length == 0 || response.values.length == 0) {
return res.notFound('No data found.')
} else {
//if(response.values.length > 200) return res.badRequest("Too many results! Maximum number of allowed results is 200")
var consumers = []
response.values.forEach(function(value){
consumers.push({
username : value[req.body.username],
custom_id : value[req.body.custom_id],
})
})
return res.json(consumers)
if(!uploadFiles.length && !fs.existsSync(CREDENTIALS_PATH + "/credentials.json")) {
if (err) return res.notFound("Credentials JSON file not found. You will need to upload one.");
}

if(uploadFiles[0]) {
fs.renameSync(uploadFiles[0].fd, CREDENTIALS_PATH + "/credentials.json");
}

var SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];

fs.readFile(CREDENTIALS_PATH + "/credentials.json", function(err, content) {
if (err) if (err) return res.negotiate(err);

var key = JSON.parse(content);


var jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES,
null
);

jwtClient.authorize(function (err, tokens) {
if (err) return res.negotiate(err)
listConsumers(jwtClient)
});

function listConsumers(auth) {
var sheets = google.sheets('v4');
sheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: req.body.spreadsheetId,
range : req.body.range,
}, function(err, response) {
if (err) return res.negotiate(err)

//var rows = response.values;
if (response.length == 0 || response.values.length == 0) {
return res.notFound('No data found.')
} else {
//if(response.values.length > 200) return res.badRequest("Too many results! Maximum number of allowed results is 200")
var consumers = []
response.values.forEach(function(value,index){
consumers.push({
username : value[req.body.username],
custom_id : value[req.body.custom_id],
})
})

// Remove first item if specified
if(req.body.ignore_first_row === 'true') {
consumers.shift()
}

return res.json(consumers)
}
});
}
})
});
}

});






}
}
Expand Down

0 comments on commit b4960c7

Please sign in to comment.