if you give me an example of calculation and teach me how to modify, I will customize the calculations.
To start the calculation examples are:
J16=(I16/(B16-B15))(C16-C15)
K16=(I16/(B16-B15))(D16-D15)
If you want to give me some simple examples of operations (+, -, /, *) so I understand how the script works.
Thanks
It will always be 0 so that I don't enter data in the cells B16, C16, D16 and I16.
Another solution could be to insert directly into cells J16 and K16 the formula "I16 / (B16-B15)) (D16-D15)" so that the spreadsheet of gsheet. But the row number increases every time I insert a new record.
For example, insert the formula as text inside the cell.
My understanding was that you wanted to run calculations on the last two rows (the ones with data in them already) In your case this would be rows 14 and 15 with this data:
I have written a script for you to add to your "CREATE" function that will do the calculation on the last two lines of the data, after you add a new row, and then return the results to your app.
// Create/Add new record, using comma separated values
if ( fn == 'CREATE' ) {
var data = e.parameter.DATA.split(',');
sh.appendRow(data);
var lr = sh.getLastRow();
var lrdata = sh.getRange(lr-1,1,2,9).getValues();
var jData = ((lrdata[1][8] / (lrdata[1][1] - lrdata[0][1])) * (lrdata[1][2] - lrdata[0][2])).toFixed(2);
var kData = ((lrdata[1][8] / (lrdata[1][1] - lrdata[0][1])) * (lrdata[1][3] - lrdata[0][3])).toFixed(2);
return ContentService.createTextOutput(JSON.stringify("New record created, " + jData + ", " + kData);
}
I try to insert your script inside the one I am using. Where do I post it?
I am attaching the scrip I am using now.
function doGet(e) {
var ss = SpreadsheetApp.openById(e.parameter.ID);
var sh = ss.getSheetByName(e.parameter.SH);
var fn = e.parameter.FN;
var rg = sh.getDataRange().getValues();
// Create/Add new record, using comma separated values
if ( fn == 'CREATE' ) {
var data = e.parameter.DATA.split(',');
sh.appendRow(data);
return ContentService.createTextOutput("New record created");
}
// Reads/Returns all data as a stringified JSON List
else if ( fn == 'READ' ) {
return ContentService.createTextOutput(JSON.stringify(rg));
}
// Edit/Update existing record, requires index/row and current col1 to match
else if ( fn == 'UPDATE' ) {
var index = e.parameter.INDEX; //index in list
var col1 = e.parameter.COL1; // current/existing value of col1 - it could be replaced...
var data = e.parameter.DATA.split(','); //new data
var range = sh.getRange((parseInt(index)+1),1,1,data.length);
for (var i = 0; i < rg.length; i++ ) {
if ( index != undefined && i == index && col1 == rg[i][0] ) {
range.setValues([data]);
}
}
return ContentService.createTextOutput("Record updated");
}
// deletes a single record (and its row) from sheet. Requires row index and col1 to match
else if ( fn == 'DELETE' ) {
var index = e.parameter.INDEX; //index in list
var col1 = e.parameter.COL1; // current/existing value of col1 - it could be replaced...
for (var i = 0; i < rg.length; i++ ) {
if ( index != undefined && i == index && col1 == rg[i][0] ) {
sh.deleteRow(parseInt(index)+1);
}
}
return ContentService.createTextOutput("Existing record deleted");
}
// outputs results from SQL query of all data
else if ( fn == 'QUERY' ) {
var rgq = sh.getName() + "!" + sh.getDataRange().getA1Notation();
var sql = e.parameter.SQL;
var qry = '=query(' + rgq + ';\"' + sql + '\";1)';
var ts = ss.insertSheet();
var setQuery = ts.getRange(1,1).setFormula(qry);
var getResult = ts.getDataRange().getValues();
ss.deleteSheet(ts);
return ContentService.createTextOutput(JSON.stringify(getResult));
}
}
function doGet(e) {
var ss = SpreadsheetApp.openById(e.parameter.ID);
var sh = ss.getSheetByName(e.parameter.SH);
var fn = e.parameter.FN;
var rg = sh.getDataRange().getValues();
if ( fn == 'CREATE' ) {
var data = e.parameter.DATA.split(',');
sh.appendRow(data);
var lr = sh.getLastRow();
var lrdata = sh.getRange(lr-1,1,2,9).getValues();
var jData = ((lrdata[1][8] / (lrdata[1][1] - lrdata[0][1])) * (lrdata[1][2] - lrdata[0][2])).toFixed(2);
var kData = ((lrdata[1][8] / (lrdata[1][1] - lrdata[0][1])) * (lrdata[1][3] - lrdata[0][3])).toFixed(2);
return ContentService.createTextOutput(JSON.stringify("New record created, " + jData + ", " + kData));
}
// Reads/Returns all data as a stringified JSON List
else if ( fn == 'READ' ) {
return ContentService.createTextOutput(JSON.stringify(rg));
}
// Edit/Update existing record, requires index/row and current col1 to match
else if ( fn == 'UPDATE' ) {
var index = e.parameter.INDEX; //index in list
var col1 = e.parameter.COL1; // current/existing value of col1 - it could be replaced...
var data = e.parameter.DATA.split(','); //new data
var range = sh.getRange((parseInt(index)+1),1,1,data.length);
for (var i = 0; i < rg.length; i++ ) {
if ( index != undefined && i == index && col1 == rg[i][0] ) {
range.setValues([data]);
}
}
return ContentService.createTextOutput("Record updated");
}
// deletes a single record (and its row) from sheet. Requires row index and col1 to match
else if ( fn == 'DELETE' ) {
var index = e.parameter.INDEX; //index in list
var col1 = e.parameter.COL1; // current/existing value of col1 - it could be replaced...
for (var i = 0; i < rg.length; i++ ) {
if ( index != undefined && i == index && col1 == rg[i][0] ) {
sh.deleteRow(parseInt(index)+1);
}
}
return ContentService.createTextOutput("Existing record deleted");
}
// outputs results from SQL query of all data
else if ( fn == 'QUERY' ) {
var rgq = sh.getName() + "!" + sh.getDataRange().getA1Notation();
var sql = e.parameter.SQL;
var qry = '=query(' + rgq + ';\"' + sql + '\";1)';
var ts = ss.insertSheet();
var setQuery = ts.getRange(1,1).setFormula(qry);
var getResult = ts.getDataRange().getValues();
ss.deleteSheet(ts);
return ContentService.createTextOutput(JSON.stringify(getResult));
}
}
The columns should automatically populate with the correct data
When you add a new row of data, this will be added to the bottom, and the formulas will provide new values
As before you will have to post values to columns B,C,D and I for this to work. You won't get a value in row 2 because there is no value in row 1 to do calculations on.