object-to-google-spreadsheet
Advanced tools
Comparing version 1.0.13 to 1.0.14
181
index.js
const ArrayToGoogleSheets = require('array-to-google-sheets'); | ||
const Joi = require('joi'); | ||
function jsonToGssFormat(obj, rowName, prop, a1, sort) { | ||
const inputSchema = Joi.object().keys({ | ||
docs: Joi.array().items(Joi.object()).required(), | ||
options: Joi.object().keys({ | ||
sheetName: Joi.string(), | ||
rowName: Joi.string(), | ||
properties: Joi.string(), | ||
a1Field: [Joi.string(), Joi.number()], | ||
sort: Joi.boolean(), | ||
removeBase: Joi.boolean() | ||
}).optional(), | ||
auth: Joi.object().keys({ | ||
creds: Joi.object().required(), | ||
docKey: Joi.string().required() | ||
}).required(), | ||
}).required(); | ||
// instantiating the set of the fields names | ||
let firstRowSet = new Set(); | ||
function jsonToGssFormat({ docs, rowName, properties, a1Field, sort }) { | ||
const firstRowSet = new Set(); | ||
docs.forEach(inpObject => Object.keys(inpObject[properties]) | ||
.forEach(keyName => firstRowSet.add(keyName))); | ||
let header = Array.from(firstRowSet); | ||
header = sort ? header.sort() : header; | ||
header.unshift(a1Field); | ||
// extracting the field names from the object | ||
obj.forEach(x => Object.keys(x[prop]).forEach(y => firstRowSet.add(y))); | ||
const fillData = docs.map(inpObject => { | ||
return header.map(uniqueKey => { | ||
return uniqueKey == a1Field ? inpObject[rowName] | ||
: inpObject[properties][uniqueKey] ? arrSringify(inpObject[properties][uniqueKey]) | ||
: false; | ||
}); | ||
}); | ||
// converting the field names set into an array | ||
const a = Array.from(firstRowSet); | ||
return [header, ...fillData]; | ||
} | ||
// sorting it if the sort option was set to true | ||
let head = sort ? a.sort() : a; | ||
function arrSringify(input) { | ||
return input instanceof Array ? input.join(', ') : input; | ||
} | ||
// assinging the value of the A1 field | ||
head.unshift(a1); | ||
function cleanChars(input) { | ||
return typeof input == 'string' ? input.replace(/[\u0000-\u001f]/g, '') : input; | ||
} | ||
// mapping the values into their corresponding rows | ||
const fillData = obj.map(x => head.map(y => y == a1 ? x[rowName] : x[prop][y] ? b(x[prop][y]) : false)); | ||
return [head, ...fillData]; | ||
}; | ||
// check if input is array, stringify accordingly | ||
function b(inp) { | ||
return inp instanceof Array ? inp.join(", ") : inp; | ||
function pushToSheet(auth, data) { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const a2gs = new ArrayToGoogleSheets(auth.docKey, auth.creds); | ||
const options = { margin: 2, minRow: 10, minCol: 10, resize: true, clear: false }; | ||
await a2gs.updateGoogleSheets(data.sheetName, data.values, options); | ||
resolve({ | ||
ok: 1, | ||
rowCount: data.values.length | ||
}); | ||
} catch (error) { | ||
reject({ | ||
error | ||
}); | ||
} | ||
}); | ||
} | ||
// catch and throw error | ||
function er(cond, msg) { | ||
if (cond) throw new Error(msg); | ||
}; | ||
// clean input out of invalid string charchters | ||
function c(inp) { | ||
return typeof inp == 'string' ? inp.replace(/[\u0000-\u001f]/g,'') : inp; | ||
} | ||
module.exports = class { | ||
constructor(creds, docKey) { | ||
this.creds = creds; | ||
this.docKey = docKey; | ||
} | ||
push(docs, options) { | ||
const creds = this.creds; | ||
const docKey = this.docKey; | ||
return new Promise((resolve, reject) => { | ||
/** | ||
* Instantiate an instance of the module | ||
* @param {*} creds - your Google's service account credintials. | ||
* @param {*} docKey - the ID of your spreadsheet. | ||
*/ | ||
constructor(creds, docKey) { | ||
this.auth = { | ||
creds, | ||
docKey | ||
}; | ||
} | ||
/** | ||
* Push your data to the sheet | ||
* @param {*} docs - the array of your objects | ||
* @param {*} options | ||
* @param {string} options.sheetName - the name of your sheet. | ||
* @param {string} options.rowName - the key name of the field that will be used as an ID. | ||
* @param {string} options.properties - the key name of the field containing the properties. | ||
* @param {string} options.a1Field - the A1 spreadsheet cell. | ||
* @param {boolean} options.removeBase - Whether or not to remove the set of IDs used to map the objects. | ||
* @returns {object} - confirmation of completion. | ||
*/ | ||
push(docs, options = {}) { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const { auth } = this; | ||
const validation = Joi.validate({ | ||
docs, | ||
options, | ||
auth: this.auth | ||
}, inputSchema); | ||
if (validation.error) { | ||
reject(validation.error); | ||
return; | ||
} | ||
const { | ||
sheetName = 'New Sheet', | ||
rowName = Object.keys(docs[0])[0], | ||
properties = Object.keys(docs[0])[1], | ||
a1Field = ' ', | ||
sort, | ||
removeBase | ||
} = options; | ||
let values = jsonToGssFormat({ docs, rowName, properties, a1Field, sort }); | ||
values = values.map(x => x.map(y => cleanChars(y))); | ||
values = removeBase ? values.map(x => x.slice(1)) : values; | ||
er(!(docs instanceof Array), "Wrong object schema provided, input must be an array of objects"); | ||
const result = await pushToSheet(auth, {values, sheetName}); | ||
resolve(result); | ||
const {sheetName, sort, removeBase} = options; | ||
er(!sheetName, "sheetName was not provided"); | ||
// if rowName, properties or a1 field were not provided get them automatically | ||
const rowName = options.rowName || Object.keys(docs[0])[0]; | ||
const properties = options.properties || Object.keys(docs[0])[1]; | ||
const a1Field = options.a1Field || " "; | ||
let repData = jsonToGssFormat(docs, rowName, properties, a1Field, sort); | ||
// clean the data | ||
repData = repData.map(x => x.map(y => c(y))); | ||
// remove the base column if removeBase is set to true | ||
repData = removeBase ? repData.map(x => x.slice(1)) : repData; | ||
// push to the API | ||
const a2gs = new ArrayToGoogleSheets(this.docKey, this.creds); | ||
const a2gsOpts = {margin: 2, minRow: 10, minCol: 10, resize: true, clear: false}; | ||
a2gs.updateGoogleSheets(sheetName, repData, a2gsOpts) | ||
.then(result => { | ||
resolve({ | ||
status: "Pushed to the spreadsheet" | ||
}); | ||
}) | ||
.catch(err => { | ||
reject(err); | ||
}); | ||
} | ||
);} | ||
} catch (error) { | ||
reject(error); | ||
} | ||
} | ||
); | ||
} | ||
}; |
{ | ||
"name": "object-to-google-spreadsheet", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"description": "Push an array of unstructured data objects into a Google spreadsheet", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
"test": "mocha", | ||
"pretest": "eslint --ignore-path .gitignore .", | ||
"coverage": "nyc mocha" | ||
}, | ||
@@ -24,9 +26,12 @@ "repository": { | ||
"dependencies": { | ||
"array-to-google-sheets": "^1.0.0" | ||
"array-to-google-sheets": "^1.0.1", | ||
"joi": "^13.2.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^4.1.2", | ||
"jest": "^22.4.2", | ||
"eslint": "^4.19.1", | ||
"mocha": "^5.1.1", | ||
"nyc": "^11.7.1", | ||
"rewire": "^3.0.2" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11694
7
244
2
5
1
+ Addedjoi@^13.2.0
+ Addedhoek@5.0.46.1.3(transitive)
+ Addedisemail@3.2.0(transitive)
+ Addedjoi@13.7.0(transitive)
+ Addedtopo@3.0.3(transitive)