Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@notainc/google-spreadsheet-as-promised
Advanced tools
Basic Google Spreadsheet functionality - load a spreadsheet, switch workbooks, read and write cells, wrapped in Promise interface and with human readable cell designation.
This is forked project. Original is https://github.com/ranhalprin/node-google-spreadsheet-as-promised
This node.js module allows connecting to a Google Spreadsheet, changing cell values and reading cell values. It is useful in the context where a spreadsheet has complicated logic that you do not want to move into code, or if you want to validate that your code behaves the same as the spreadsheet for different inputs.
npm install @notainc/google-spreadsheet-as-promised
The following usage example covers most provided functionality. It shows how to change some parameter cells in the spreadsheet and then read values from other cells.
var GoogleSpreadsheetAsPromised = require('google-spreadsheet-as-promised');
var CREDS = require('./google-api-creds.json');
var SPREADSHEET_KEY = '<spreadsheet key>';
var WORKSHEET_ID = 0;
var PARAMETER_RANGE = 'B2:C8'; // Matrix range covers all cells where parameters should be changed
var PARAMETER_CELLS = {
parameter1: 'B2',
parameter2: 'B4',
parameter3: 'C7',
parameter4: 'C8'
};
var TOTAL_CELL = 'D20';
var INTERMEDIATE_CALCULATIONS_RANGE = 'D10:D14';
var parameters = {
parameter1: '100',
parameter2: '0.5',
parameter3: '17',
parameter4: '55'
};
getResultWithParameter(parameters).then(function(result) {
console.log(result);
});
// Given parameters, resolves to the result cell after
function getResultWithParameter(parameters) {
return new Promise(function(resolve, reject) {
var sheet = new GoogleSpreadsheetAsPromised();
var worksheet;
var result = {};
sheet.load(SPREADSHEET_KEY, CREDS).then(function() {
return sheet.getWorksheet(WORKSHEET_ID);
}).then(function(resultWorksheet) {
worksheet = resultWorksheet;
return worksheet.getCells(PARAMETER_RANGE);
}).then(function(cells) {
var promises = [];
var names = Object.keys(parameters);
for (var i = 0; i < names.length; i++) {
var name = names[i];
var value = parameters[name];
var cell = PARAMETER_CELLS[name];
if (!cell) {
return reject("Unknown Parameter: " + name);
}
promises.push(cells.setValue(cell, value));
}
return Promise.all(promises); // This makes all values change in parallel
}).then(function() {
return worksheet.getCell(TOTAL_CELL); // We must load the result cell only after parameter values are all set
}).then(function(cell) {
result.total = cell.getValue();
return worksheet.getCells(INTERMEDIATE_CALCULATIONS_RANGE);
}).then(function(cells) {
result.intermediate_values_array = cells.getAllValues();
return resolve(result);
});
});
};
new GoogleSpreadsheetAsPromised()
Create a new google spreadsheet object.
Load must be called before any other functionality is available. Resolves to undefined.
sheet_id
-- the ID of the spreadsheet (from its URL)creds
-- the credential needed to access the spreadsheet (see the "Authentication" section)Resolves to a WorksheetAsPromised object that represents one of the worksheets (tabs) in the spreadsheet.
worksheet_id
-- the numeric ID of the worksheet you want to work with, usually 0 for the default tab.Resolves to a WorksheetAsPromised object that represents one of the worksheets (tabs) in the spreadsheet.
WorksheetAsPromised
This class represnts a worksheet (tab)
Resolves to a CellsAsPromised object that represents a groups of cells.
-- range
-- a string representation of the cell range to load in the form '<min-col><min-row>:<max-col><max-row>'
for example 'A1:B4' will load the eight cells 'A1','A2','A3','A4','B1','B2','B3','B4'
Resolves to a CellAsPromised object that represents a single cells.
-- cell
-- a string representation of the cell in the form '<col><row>'
, for example 'B2'
Return the value in a given cell.
-- cell
-- a string representation of the cell in the form '<col><row>'
, for example 'B2'
Return all the values loaded in a single array built from the data horizontally first and then vertically. For example, if loaded the range 'A1:B2' it will return the values in the order 'A1','A2','B1','B2'.
Returns the width (number of columns) of the cell group loaded.
Returns the height (number of rows) of the cell group loaded.
Sets a value to a cell in the spreadsheet. Resolves to undefined.
-- cell
-- a string representation of the cell in the form '<col><row>'
, for example 'B2'
-- value
-- a string value to write into the cell
Returns the value in the cell.
Sets a value to the cell. Resolves to undefined.
-- value
-- a string value to write into the cell
The recommended authentication method is to give a google api service access to the spreadsheet. There is no need to create a new user for the module to use it.
(More about Service Authentication)
Setup Instructions
If you are using heroku or another environment where you cannot save a local file, the recommended method is to save the two required fields into enviornment variables:
client_email
-- your service account's email addressprivate_key
-- the private key found in the JSON fileAnd then load them in runtime like so:
creds.private_key = process.env.GOOGLE_API_KEY.replace(/\\n/g,"\n"); // Newlines are escaped in env variables
creds.client_email = process.env.GOOGLE_API_EMAIL;
This module wraps the google-spreadsheet nodejs module. It provides two changes in API:
NOTE: The wrapper is very limited and supports only a limited set of functionality the exists in the original module.
If you need more features or find bugs, please contribute your improvements through pull requests in our github. Please also update usage.js with an example for the new functionality example.
In order to maintain compatability this wrapper is locked to a specific version of the google-spreadsheet module.
Support bulk setting of multiple cells in one API call to reduce network overhead and response time.
google-spreadsheets-as-promised is free and unencumbered public domain software. For more information, see the accompanying UNLICENSE file.
FAQs
Basic Google Spreadsheet functionality - load a spreadsheet, switch workbooks, read and write cells, wrapped in Promise interface and with human readable cell designation.
The npm package @notainc/google-spreadsheet-as-promised receives a total of 4 weekly downloads. As such, @notainc/google-spreadsheet-as-promised popularity was classified as not popular.
We found that @notainc/google-spreadsheet-as-promised demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.