Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mastef committed Mar 8, 2018
1 parent 6c5527a commit 6f50866
Showing 1 changed file with 146 additions and 149 deletions.
295 changes: 146 additions & 149 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,182 +528,179 @@ var SpreadsheetRow = function( spreadsheet, data, xml ){
}

function SpreadsheetCell(spreadsheet, ss_key, worksheet_id, data){
var links;
this.row = parseInt(data['gs:cell']['$']['row']);
this.col = parseInt(data['gs:cell']['$']['col']);
this.batchId = 'R'+this.row+'C'+this.col;
if(data['id'] == "https://spreadsheets.google.com/feeds/cells/" + ss_key + "/" + worksheet_id + '/' + this.batchId) {
this.ws_id = worksheet_id;
this.ss = ss_key;
}else{
this.id = data['id'];
}
var links;
this.row = parseInt(data['gs:cell']['$']['row']);
this.col = parseInt(data['gs:cell']['$']['col']);
this.batchId = 'R'+this.row+'C'+this.col;
if(data['id'] == "https://spreadsheets.google.com/feeds/cells/" + ss_key + "/" + worksheet_id + '/' + this.batchId) {
this.ws_id = worksheet_id;
this.ss = ss_key;
}else{
this.id = data['id'];
}

this['_links'] = [];
links = forceArray( data.link );
for (var i = 0; i < links.length; i++) {
link = links[i];
if(link['$']['rel'] == "self" && link['$']['href'] == this.getSelf()) continue;
if(link['$']['rel'] == "edit" && link['$']['href'] == this.getEdit()) continue;
this['_links'][ link['$']['rel'] ] = link['$']['href'];
};
if(this['_links'].length == 0) delete this['_links'];
this['_links'] = [];
links = forceArray( data.link );
for (var i = 0; i < links.length; i++) {
link = links[i];
if(link['$']['rel'] == "self" && link['$']['href'] == this.getSelf()) continue;
if(link['$']['rel'] == "edit" && link['$']['href'] == this.getEdit()) continue;
this['_links'][ link['$']['rel'] ] = link['$']['href'];
};
if(this['_links'].length == 0) delete this['_links'];

this.updateValuesFromResponseData(data);
this.updateValuesFromResponseData(data);

return this;
return this;
};

SpreadsheetCell.prototype.getId = function() {
if(!!this.id) {
return this.id;
} else {
return "https://spreadsheets.google.com/feeds/cells/" + this.ss + "/" + this.ws_id + '/' + this.batchId;
}
}

SpreadsheetCell.prototype.getEdit = function() {
if(!!this['_links'] && !!this['_links']['edit']) {
return this['_links']['edit'];
} else {
return this.getId().replace(this.batchId, "private/full/" + this.batchId);
}
}
SpreadsheetCell.prototype.getId = function() {
if(!!this.id) {
return this.id;
} else {
return "https://spreadsheets.google.com/feeds/cells/" + this.ss + "/" + this.ws_id + '/' + this.batchId;
}
}

SpreadsheetCell.prototype.getSelf = function() {
if(!!this['_links'] && !!this['_links']['edit']) {
return this['_links']['edit'];
} else {
return this.getId().replace(this.batchId, "private/full/" + this.batchId);
}
}
SpreadsheetCell.prototype.getEdit = function() {
if(!!this['_links'] && !!this['_links']['edit']) {
return this['_links']['edit'];
} else {
return this.getId().replace(this.batchId, "private/full/" + this.batchId);
}
}

SpreadsheetCell.prototype.updateValuesFromResponseData = function(_data) {
// formula value
var input_val = _data['gs:cell']['$']['inputValue'];
// inputValue can be undefined so substr throws an error
// still unsure how this situation happens
if (input_val && input_val.substr(0,1) === '='){
this._formula = input_val;
} else {
this._formula = undefined;
}
SpreadsheetCell.prototype.getSelf = function() {
if(!!this['_links'] && !!this['_links']['edit']) {
return this['_links']['edit'];
} else {
return this.getId().replace(this.batchId, "private/full/" + this.batchId);
}
}

// numeric values
if (_data['gs:cell']['$']['numericValue'] !== undefined) {
this._numericValue = parseFloat(_data['gs:cell']['$']['numericValue']);
} else {
this._numericValue = undefined;
}
SpreadsheetCell.prototype.updateValuesFromResponseData = function(_data) {
// formula value
var input_val = _data['gs:cell']['$']['inputValue'];
// inputValue can be undefined so substr throws an error
// still unsure how this situation happens
if (input_val && input_val.substr(0,1) === '='){
this._formula = input_val;
} else {
this._formula = undefined;
}

// the main "value" - its always a string
this._value = _data['gs:cell']['_'] || '';
}
// numeric values
if (_data['gs:cell']['$']['numericValue'] !== undefined) {
this._numericValue = parseFloat(_data['gs:cell']['$']['numericValue']);
} else {
this._numericValue = undefined;
}

SpreadsheetCell.prototype.setValue = function(new_value, cb) {
this.value = new_value;
this.save(cb);
};
// the main "value" - its always a string
this._value = _data['gs:cell']['_'] || '';
}

SpreadsheetCell.prototype._clearValue = function() {
this._formula = undefined;
this._numericValue = undefined;
this._value = '';
}
SpreadsheetCell.prototype.setValue = function(new_value, cb) {
this.value = new_value;
this.save(cb);
};

Object.defineProperty(SpreadsheetCell.prototype, "value", {
get: function(){
return this._value;
},
set: function(val){
if (!val) return this._clearValue();

var numeric_val = parseFloat(val);
if (!isNaN(numeric_val)){
this._numericValue = numeric_val;
this._value = val.toString();
} else {
this._numericValue = undefined;
this._value = val;
}

if (typeof val == 'string' && val.substr(0,1) === '=') {
// use the getter to clear the value
this.formula = val;
} else {
this._formula = undefined;
}
}
});
SpreadsheetCell.prototype._clearValue = function() {
this._formula = undefined;
this._numericValue = undefined;
this._value = '';
}

Object.defineProperty(SpreadsheetCell.prototype, "formula", {
get: function() {
return this._formula;
},
set: function(val){
if (!val) return this._clearValue();

if (val.substr(0,1) !== '=') {
throw new Error('Formulas must start with "="');
}
this._numericValue = undefined;
this._value = '*SAVE TO GET NEW VALUE*';
this._formula = val;
}
});
Object.defineProperty(SpreadsheetCell.prototype, "value", {
get: function(){
return this._value;
},
set: function(val){
if (!val) return this._clearValue();

var numeric_val = parseFloat(val);
if (!isNaN(numeric_val)){
this._numericValue = numeric_val;
this._value = val.toString();
} else {
this._numericValue = undefined;
this._value = val;
}

Object.defineProperty(SpreadsheetCell.prototype, "numericValue", {
get: function() {
return this._numericValue;
},
set: function(val) {
if (val === undefined || val === null) return this._clearValue();
if (typeof val == 'string' && val.substr(0,1) === '=') {
// use the getter to clear the value
this.formula = val;
} else {
this._formula = undefined;
}
}
});

if (isNaN(parseFloat(val)) || !isFinite(val)) {
throw new Error('Invalid numeric value assignment');
}
Object.defineProperty(SpreadsheetCell.prototype, "formula", {
get: function() {
return this._formula;
},
set: function(val){
if (!val) return this._clearValue();

this._value = val.toString();
this._numericValue = parseFloat(val);
this._formula = undefined;
}
});
if (val.substr(0,1) !== '=') {
throw new Error('Formulas must start with "="');
}
this._numericValue = undefined;
this._value = '*SAVE TO GET NEW VALUE*';
this._formula = val;
}
});

Object.defineProperty(SpreadsheetCell.prototype, "valueForSave", {
get: function() {
return xmlSafeValue(this._formula || this._value);
}
});
Object.defineProperty(SpreadsheetCell.prototype, "numericValue", {
get: function() {
return this._numericValue;
},
set: function(val) {
if (val === undefined || val === null) return this._clearValue();

SpreadsheetCell.prototype.save = function(cb) {
if ( !cb ) cb = function(){};
this._needsSave = false;
if (isNaN(parseFloat(val)) || !isFinite(val)) {
throw new Error('Invalid numeric value assignment');
}

var edit_id = 'https://spreadsheets.google.com/feeds/cells/key/worksheetId/private/full/R'+this.row+'C'+this.col;
var data_xml =
'<entry><id>'+this.getId()+'</id>'+
'<link rel="edit" type="application/atom+xml" href="'+this.getId()+'"/>'+
'<gs:cell row="'+this.row+'" col="'+this.col+'" inputValue="'+this.valueForSave+'"/></entry>'
this._value = val.toString();
this._numericValue = parseFloat(val);
this._formula = undefined;
}
});

data_xml = data_xml.replace('<entry>', "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>");
Object.defineProperty(SpreadsheetCell.prototype, "valueForSave", {
get: function() {
return xmlSafeValue(this._formula || this._value);
}
});

var self = this;
SpreadsheetCell.prototype.save = function(cb) {
if ( !cb ) cb = function(){};
this._needsSave = false;

spreadsheet.makeFeedRequest( this.getEdit(), 'PUT', data_xml, function(err, response) {
if (err) return cb(err);
self.updateValuesFromResponseData(response);
cb();
});
}
var edit_id = 'https://spreadsheets.google.com/feeds/cells/key/worksheetId/private/full/R'+this.row+'C'+this.col;
var data_xml =
'<entry><id>'+this.getId()+'</id>'+
'<link rel="edit" type="application/atom+xml" href="'+this.getId()+'"/>'+
'<gs:cell row="'+this.row+'" col="'+this.col+'" inputValue="'+this.valueForSave+'"/></entry>'

SpreadsheetCell.prototype.del = function(cb) {
this.setValue('', cb);
}
data_xml = data_xml.replace('<entry>', "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>");

var self = this;

GoogleSpreadsheet.SpreadsheetCell = SpreadsheetCell;
spreadsheet.makeFeedRequest( this.getEdit(), 'PUT', data_xml, function(err, response) {
if (err) return cb(err);
self.updateValuesFromResponseData(response);
cb();
});
}

SpreadsheetCell.prototype.del = function(cb) {
this.setValue('', cb);
}

GoogleSpreadsheet.SpreadsheetCell = SpreadsheetCell;

module.exports = GoogleSpreadsheet;

Expand Down

0 comments on commit 6f50866

Please sign in to comment.