In summary, I'm trying to simplify this function that load values from two different sheets to another sheet.
All the values are stored in rows in two sheets (DBClienti and DataBkp), all these rows have a reference cell with a unique ID. I select an ID from DBClienti and the function find the relative row number, corresponding to the data to load in the last sheet (Quota).
I'm setting this data using all those vars, but of course there is a better (and right) way that I don't know.
function loadDataBkp() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheetQuota = ss.getSheetByName('Quota');
const sheetDBClienti = ss.getSheetByName("DBClienti");
const sheetDataBkp = ss.getSheetByName("DataBkp");
//Reset Inputs
resetQuota();
//Select the ID DOC
var selectedIDDoc = sheetDBClienti.getActiveCell();
var selectedIDDocVal = selectedIDDoc.getValue();
//Find row of ID DOC in DBClienti
var rowDBClienti;
const dataDBClienti = sheetDBClienti.getDataRange().getValues();
for(var i = 0; i<dataDBClienti.length;i++){
if(dataDBClienti[i][9] == selectedIDDocVal){
rowDBClienti = i+1;
}
}
//Set values in Quota - list
var valI4 = sheetDBClienti.getRange(rowDBClienti,1).getValue();
var valI5 = sheetDBClienti.getRange(rowDBClienti,2).getValue();
var valI6 = sheetDBClienti.getRange(rowDBClienti,3).getValue();
var valI7 = sheetDBClienti.getRange(rowDBClienti,4).getValue();
var valI8 = sheetDBClienti.getRange(rowDBClienti,5).getValue();
var valI9 = sheetDBClienti.getRange(rowDBClienti,6).getValue();
var valI10 = sheetDBClienti.getRange(rowDBClienti,7).getValue();
var valI11 = sheetDBClienti.getRange(rowDBClienti,8).getValue();
sheetQuota.getRange('I4').setValue(valI4);
sheetQuota.getRange('I5').setValue(valI5);
sheetQuota.getRange('I6').setValue(valI6);
sheetQuota.getRange('I7').setValue(valI7);
sheetQuota.getRange('I8').setValue(valI8);
sheetQuota.getRange('I9').setValue(valI9);
sheetQuota.getRange('I10').setValue(valI10);
sheetQuota.getRange('I11').setValue(valI11);
//Find row of ID DOC in DataBkp
var rowDataBkp;
const dataDataBkp = sheetDataBkp.getDataRange().getValues();
for(var i = 0; i<dataDataBkp.length;i++){
if(dataDataBkp[i][0] == selectedIDDocVal){
rowDataBkp = i+1;
}
}
//Set values in Quota - sections
var valC2 = sheetDataBkp.getRange(rowDataBkp,2).getValue();
var valC4 = sheetDataBkp.getRange(rowDataBkp,3).getValue();
var valC5 = sheetDataBkp.getRange(rowDataBkp,4).getValue();
var valC6 = sheetDataBkp.getRange(rowDataBkp,5).getValue();
var valC7 = sheetDataBkp.getRange(rowDataBkp,6).getValue();
var valC8 = sheetDataBkp.getRange(rowDataBkp,7).getValue();
var valC9 = sheetDataBkp.getRange(rowDataBkp,8).getValue();
var valC10 = sheetDataBkp.getRange(rowDataBkp,9).getValue();
var valC11 = sheetDataBkp.getRange(rowDataBkp,10).getValue();
var valC12 = sheetDataBkp.getRange(rowDataBkp,11).getValue();
var valF4 = sheetDataBkp.getRange(rowDataBkp,12).getValue();
var valF5 = sheetDataBkp.getRange(rowDataBkp,13).getValue();
var valF8 = sheetDataBkp.getRange(rowDataBkp,14).getValue();
var valF9 = sheetDataBkp.getRange(rowDataBkp,15).getValue();
var valF12 = sheetDataBkp.getRange(rowDataBkp,16).getValue();
var valF13 = sheetDataBkp.getRange(rowDataBkp,17).getValue();
var valF25 = sheetDataBkp.getRange(rowDataBkp,18).getValue();
var valF26 = sheetDataBkp.getRange(rowDataBkp,19).getValue();
var valF27 = sheetDataBkp.getRange(rowDataBkp,20).getValue();
var valI14 = sheetDataBkp.getRange(rowDataBkp,21).getValue();
sheetQuota.getRange('C2').setValue(valC2);
sheetQuota.getRange('C4').setValue(valC4);
sheetQuota.getRange('C5').setValue(valC5);
sheetQuota.getRange('C6').setValue(valC6);
sheetQuota.getRange('C7').setValue(valC7);
sheetQuota.getRange('C8').setValue(valC8);
sheetQuota.getRange('C9').setValue(valC9);
sheetQuota.getRange('C10').setValue(valC10);
sheetQuota.getRange('C11').setValue(valC11);
sheetQuota.getRange('C12').setValue(valC12);
sheetQuota.getRange('F4').setValue(valF4);
sheetQuota.getRange('F5').setValue(valF5);
sheetQuota.getRange('F8').setValue(valF8);
sheetQuota.getRange('F9').setValue(valF9);
sheetQuota.getRange('F12').setValue(valF12);
sheetQuota.getRange('F13').setValue(valF13);
sheetQuota.getRange('F25').setValue(valF25);
sheetQuota.getRange('F26').setValue(valF26);
sheetQuota.getRange('F27').setValue(valF27);
sheetQuota.getRange('I14').setValue(valI14);
}
Try it this way:
function loadDataBkp() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh1 = ss.getSheetByName('Quota');
const sh2 = ss.getSheetByName("DBClienti");
const sh3 = ss.getSheetByName("DataBkp");
resetQuota();
var v2 = sh2.getActiveCell().getValue();
var row1;
sh2.getDataRange().getValues().forEach((r,i) => { if (r[9] == v2) { row1 = i + 1; } })
sh1.getRange(4, 9, 8).setValues(sh2.getRange(row1, 1, 1, 8).getValues().flat().map(v => [v]))
var row2;
const vs3 = sh3.getDataRange().getValues().forEach((r, i) => { if (r[0] == v2) { row2 = i + 1 } })
let xvs = sh3.getRange(row2, 2, 1, 20).getValues().flat();
['C2', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'F4', 'F5', 'F8', 'F9', 'F12', 'F13', 'F25', 'F26', 'F27', 'I14'].forEach((s, i) => { sh1.getRange(s).setValue(xvs[i]) });
}
How can I make parent("td").prevAll("td") to be dynamic?
$("body").on("click",".edit-item",function(){
var id = $(this).parent("td").data('id');
var name = $(this).parent("td").prevAll("td").eq(9).text();
var email = $(this).parent("td").prevAll("td").eq(8).text();
var phone = $(this).parent("td").prevAll("td").eq(7).text();
var CNP = $(this).parent("td").prevAll("td").eq(6).text();
var birthday = $(this).parent("td").prevAll("td").eq(5).text();
var date = $(this).parent("td").prevAll("td").eq(4).text();
var speciality_id = $(this).parent("td").prevAll("td").eq(3).text();
var doctor_id = $(this).parent("td").prevAll("td").eq(2).text();
var location_id = $(this).parent("td").prevAll("td").eq(1).text();
var message = $(this).parent("td").prevAll("td").eq(0).text();
$("#edit-item").find("input[name='name']").val(name);
$("#edit-item").find("input[name='email']").val(email);
$("#edit-item").find("input[name='phone']").val(phone);
$("#edit-item").find("input[name='CNP']").val(CNP);
$("#edit-item").find("input[name='birthday']").val(birthday);
$("#edit-item").find("input[name='date']").val(date);
$("#edit-item").find("select[name='speciality_id']").val(speciality_id);
$("#edit-item").find("select[name='doctor_id']").val(doctor_id);
$("#edit-item").find("select[name='location_id']").val(location_id);
$("#edit-item").find("textarea[name='message']").val(message);
$("#edit-item").find("form").attr("action",url + '/' + id);
});
I have a problem debugging the code. It's working if I define values by myself instead of taking it from properties and spreadsheet. I am new to JavaScript so it can be really basic error.
What I am trying to do with this function:
Taking name of person, date and name of school from the spreadsheet
Getting arrays data from the properties that was saved with other function. The saving line in that function looks like this:
PropertiesService.getScriptProperties().setProperty('Mokyklos', JSON.stringify(Mokyklos));
It's 6 arrays of full names, array of date, array of school names and array of numbers. The numbers array is used to return the answer.
function ApmokMokMokykloje(mokytojas, data, mokykla) {
Utilities.sleep(Math.random() * 1000);
var MokytojaiL = PropertiesService.getScriptProperties().getProperty('MokytojaiL');
var Mokytojai1 = PropertiesService.getScriptProperties().getProperty('Mokytojai1');
var Mokytojai2 = PropertiesService.getScriptProperties().getProperty('Mokytojai2');
Utilities.sleep(Math.random() * 1000);
var Mokytojai3 = PropertiesService.getScriptProperties().getProperty('Mokytojai3');
var Mokytojai4 = PropertiesService.getScriptProperties().getProperty('Mokytojai4');
var Mokytojai5 = PropertiesService.getScriptProperties().getProperty('Mokytojai5');
Utilities.sleep(Math.random() * 1000);
var Datos = PropertiesService.getScriptProperties().getProperty('Datos');
var Mokyklos = PropertiesService.getScriptProperties().getProperty('Mokyklos');
var ApmokMokSkaiciai = PropertiesService.getScriptProperties().getProperty('ApmokMokSkaiciai');
var mokytojaiL = MokytojaiL;
mokytojaiL = JSON.parse(mokytojaiL);
var mokytojai1 = Mokytojai1;
mokytojai1 = JSON.parse(mokytojai1);
var mokytojai2 = Mokytojai2;
mokytojai2 = JSON.parse(mokytojai2);
var mokytojai3 = Mokytojai3;
mokytojai3 = JSON.parse(mokytojai3);
var mokytojai4 = Mokytojai4;
mokytojai4 = JSON.parse(mokytojai4);
var mokytojai5 = Mokytojai5;
mokytojai5 = JSON.parse(mokytojai5);
var datos = Datos;
datos = JSON.parse(datos);
var mokyklos = Mokyklos;
mokyklos = JSON.parse(mokyklos);
var skaicius = ApmokMokSkaiciai;
skaicius = JSON.parse(skaicius);
for(var i = 0; i < mokyklos.length; i++) {
if(data==datos[i] && mokykla==mokyklos[i]) {
if ((mokytojas==mokytojaiL[i]) || (mokytojas==mokytojai1[i]) || (mokytojas==mokytojai2[i]) || (mokytojas==mokytojai3[i]) || (mokytojas==mokytojai4[i]) || (mokytojas==mokytojai5[i])) {
return skaicius[i]; // returns blank
}
}
}
}
The code below works fine.
function testas3(mokytojas, data, mokykla) {
// the same values from spreadsheet
var datapvz="2017-11-04T22:00:00.000Z";
var mokyklapvz="Zalioji mokykla";
var mokytojaspvz="Penivilas Gremlinavičius";
// the same values from properties
var datos = [];
datos[0]="2017-11-08T22:00:00.000Z";
datos[1] = "2017-11-15T22:00:00.000Z";
datos[2] = "2017-11-11T22:00:00.000Z";
datos[3] = "2017-11-03T22:00:00.000Z";
datos[4] = "2017-11-04T22:00:00.000Z";
var mokyklos = [];
mokyklos[0] = "Mokykla nuostabioji";
mokyklos[1] = "Mylimiausioji mokyklele";
mokyklos[2] = "Dar viena mokyykla";
mokyklos[3] = "Raudonoji";
mokyklos[4] = "Zalioji mokykla";
var mokytojaiL = [];
mokytojaiL[0] = "Cristopher Rangel";
mokytojaiL[1] = "Alessandra Knox";
mokytojaiL[2] = "Germtautas Falalavičius";
mokytojaiL[3] = "Lenkgaudė Trikojytė";
mokytojaiL[4] = "Penivilas Gremlinavičius";
var mokytojai1 = [];
mokytojai1[0] = "Mantvydas Špukys";
mokytojai1[1] = "Išeikbaida Visursėkmytė";
mokytojai1[2] = "Svaidgaudė Praperduvienė";
mokytojai1[3] = "Mantvinas Žirgmyla";
mokytojai1[4] = "Mantvinas Žirgmyla";
var mokytojai2 = [];
mokytojai2[0] = "Griovbaida Nepriteklytė";
mokytojai2[1] = "Išeikbaida Visursėkmytė";
mokytojai2[2] = "Griovbaida Nepriteklytė";
mokytojai2[3] = "Arjautauta Fragmentavičiutė";
mokytojai2[4] = "Kastuvaldas Parašiutauskas";
var mokytojai3 = [];
mokytojai3[0] = "Rustautas Celiulionis";
mokytojai3[1] = "Androbauda Parankpapaitė";
mokytojai3[2] = "Arjauvilė Katrakojytė";
mokytojai3[3] = null;
mokytojai3[4] = "Rustautas Celiulionis";
var mokytojai4 = [];
mokytojai4[0] = null;
mokytojai4[1] = null;
mokytojai4[2] = null;
mokytojai4[3] = "Arjauvilė Katrakojytė";
var mokytojai5 = [];
var skaicius = [];
skaicius[0]="99";
skaicius[1]="98";
skaicius[2]="87";
skaicius[3]="89";
skaicius[4]="89";
for(var i = 0; i < mokyklos.length; i++) {
if(datapvz==datos[i] && mokyklapvz==mokyklos[i]) {
if ((mokytojaspvz==mokytojaiL[i]) || (mokytojaspvz==mokytojai1[i]) || (mokytojaspvz==mokytojai2[i]) || (mokytojaspvz==mokytojai3[i]) || (mokytojaspvz==mokytojai4[i]) || (mokytojaspvz==mokytojai5[i]))
{
return skaicius[i]; // returns 89
}
}
}
}
I think the problem is that you're comparing a Date object (from the spreadsheet...e.g., from cell F$1) with a string literal (from your saved JSON object). To make them match, you should call toJSON() on the date object before comparing, like this:
var date = data.toJSON();
for(var i = 0; i < mokyklos.length; i++) {
if(date==datos[i] && mokykla==mokyklos[i]) {
if ((mokytojas==mokytojaiL[i]) || (mokytojas==mokytojai1[i]) || (mokytojas==mokytojai2[i]) || (mokytojas==mokytojai3[i]) || (mokytojas==mokytojai4[i]) || (mokytojas==mokytojai5[i])) {
return skaicius[i];
}
}
}
This question already has answers here:
Why does Firebug say toFixed() is not a function?
(7 answers)
Closed 5 years ago.
...var totalFirst = bookFirst+exFirst; totalFirst = totalFirst.toFixed(2);
what is wrong with the code?
{
var layStakeWin = (backStake*(backOdds-1.0)*(1.0-backCom)+backStake)/(layOdds-layCom);
layStakeWin = layStakeWin.toFixed(2);
var layStakePlace = (backStake*(((backOdds-1.0)/placeTerms)+1)*(1.0-backCom))/(layOdds-layCom);
layStakePlace = layStakePlace.toFixed(2);
var bookFirst = (backStake*(backOdds-1.0)*(1.0-backCom))+(backStake*(backOdds-1.0)/placeTerms*(1.0-backCom));
bookFirst = bookFirst.toFixed(2);
var exFirst = 0-(layStakeWin*(layOdds-1.0))-(layStakePlace*((layOdds/placeTerms)-1.0));
exFirst = exFirst.toFixed(2);
var totalFirst = bookFirst+exFirst;
totalFirst = totalFirst.toFixed(2);
var bookStandard = 0-backStake+(backStake*(backOdds-1.0)/placeTerms*(1-backCom));
bookStandard = bookStandard.toFixed(2);
var exStandard = (layStakeWin*(1-layCom))-(layStakePlace*(layOdds-1.0));
exStandard = exStandard.toFixed(2);
var totalStandard = bookStandard+exStandard;
totalStandard = totalStandard.toFixed(2);
var bookExtra = 0-backStake+(backStake*(backOdds-1.0)/placeTerms*(1.0-backCom));
bookExtra = bookExtra.toFixed(2);
var exExtra = (layStakeWin*(1-layCom))+(layStakePlace*(layOdds-1.0));
exExtra = exExtra.toFixed(2);
var totalExtra = bookExtra+exExtra;
totalExtra = totalExtra.toFixed(2);
var bookNo = 0-backStake-backStake;
bookNo = bookNo.toFixed(2);
var exNo = (layStake*(1.0-layCom))+(layStakePlace*(layOdds-1.0));
exNo = exNo.toFixed(2);
var totalNo = bookNo+exNo;
totalNo = totalNo.toFixed(2);
}
The problem is that toFixed() returns a string.
To fix this problem, everywhere you use toFixed(), wrap it inside the parseFloat() function.
For example,
layStakeWin = parseFloat(layStakeWin).toFixed(2);
Please make this change everywhere you use toFixed().
{
var layStakeWin = (backStake*(backOdds-1.0)*(1.0-backCom)+backStake)/(layOdds-layCom);
layStakeWin = parseFloat(layStakeWin).toFixed(2);
var layStakePlace = (backStake*(((backOdds-1.0)/placeTerms)+1)*(1.0-backCom))/(layOdds-layCom);
layStakePlace = parseFloat(layStakePlace).toFixed(2);
var bookFirst = (backStake*(backOdds-1.0)*(1.0-backCom))+(backStake*(backOdds-1.0)/placeTerms*(1.0-backCom));
bookFirst = parseFloat(bookFirst).toFixed(2);
var exFirst = 0-(layStakeWin*(layOdds-1.0))-(layStakePlace*((layOdds/placeTerms)-1.0));
exFirst = parseFloat(exFirst).toFixed(2);
var totalFirst = bookFirst+exFirst;
totalFirst = totalFirst.toFixed(2);
var bookStandard = 0-backStake+(backStake*(backOdds-1.0)/placeTerms*(1-backCom));
bookStandard = parseFloat(bookStandard).toFixed(2);
var exStandard = (layStakeWin*(1-layCom))-(layStakePlace*(layOdds-1.0));
exStandard = parseFloat(exStandard).toFixed(2);
var totalStandard = bookStandard+exStandard;
totalStandard = totalStandard.toFixed(2);
var bookExtra = 0-backStake+(backStake*(backOdds-1.0)/placeTerms*(1.0-backCom));
bookExtra = parseFloat(bookExtra).toFixed(2);
I'm trying to use javascript to perform calculations on a button click because I don't want the page to refresh when this button is clicked. I wrote a script below:
var name = document.getElementById('recipeName').value;
var lvl = document.getElementById('recipeLvl').value;
var e = document.getElementById("qualitySelect");
var quality = e.options[e.selectedIndex].value;
e = document.getElementById("classSelect");
var craft = e.options[e.selectedIndex].value;
var mat1 = document.getElementById('material1Name').value;
var mat1Qty = document.getElementById('material1Qty').value;
var mat1NQ = document.getElementById('material1NQ').value;
var mat1NQprice = document.getElementById('material1NQprice').value;
var mat1HQ = document.getElementById('material1HQ').value;
var mat1HQprice = document.getElementById('material1HQprice').value;
var mat2 = document.getElementById('material2Name').value;
var mat2Qty = document.getElementById('material2Qty').value;
var mat2NQ = document.getElementById('material2NQ').value;
var mat2NQprice = document.getElementById('material2NQprice').value;
var mat2HQ = document.getElementById('material2HQ').value;
var mat2HQprice = document.getElementById('material2HQprice').value;
var mat3 = document.getElementById('material3Name').value;
var mat3Qty = document.getElementById('material3Qty').value;
var mat3NQ = document.getElementById('material3NQ').value;
var mat3NQprice = document.getElementById('material3NQprice').value;
var mat3HQ = document.getElementById('material3HQ').value;
var mat3HQprice = document.getElementById('material3HQprice').value;
var mat4 = document.getElementById('material4Name').value;
var mat4Qty = document.getElementById('material4Qty').value;
var mat4NQ = document.getElementById('material4NQ').value;
var mat4NQprice = document.getElementById('material4NQprice').value;
var mat4HQ = document.getElementById('material4HQ').value;
var mat4HQprice = document.getElementById('material4HQprice').value;
var mat5 = document.getElementById('material5Name').value;
var mat5Qty = document.getElementById('material5Qty').value;
var mat5NQ = document.getElementById('material5NQ').value;
var mat5NQprice = document.getElementById('material5NQprice').value;
var mat5HQ = document.getElementById('material5HQ').value;
var mat5HQprice = document.getElementById('material5HQprice').value;
var mat6 = document.getElementById('material6Name').value;
var mat6Qty = document.getElementById('material6Qty').value;
var mat6NQ = document.getElementById('material6NQ').value;
var mat6NQprice = document.getElementById('material6NQprice').value;
var mat6HQ = document.getElementById('material6HQ').value;
var mat6HQprice = document.getElementById('material6HQprice').value;
e = document.getElementById("catalyst1");
var crystal1 = e.options[e.selectedIndex].value;
e = document.getElementById('element1');
var element1 = e.options[e.selectedIndex].value;
var crystal1Qty = document.getElementById('crystalQty1').value;
var crystal1Price = document.getElementById('crystalPrice1').value;
e = document.getElementById("catalyst2");
var crystal2 = e.options[e.selectedIndex].value;
e = document.getElementById('element2');
var element2 = e.options[e.selectedIndex].value;
var crystal2Qty = document.getElementById('crystalQty2').value;
var crystal2Price = document.getElementById('crystalPrice2').value;
var notes = document.getElementById('notes').value;
var marketPrice = document.getElementById('marketPrice').value;
function calculate() {
var mat1Cost = (mat1NQ * mat1NQprice) + (mat1HQ * mat1HQprice);
var mat2Cost = (mat2NQ * mat2NQprice) + (mat2HQ * mat2HQprice);
var mat3Cost = (mat3NQ * mat3NQprice) + (mat3HQ * mat3HQprice);
var mat4Cost = (mat4NQ * mat4NQprice) + (mat4HQ * mat4HQprice);
var mat5Cost = (mat5NQ * mat5NQprice) + (mat5HQ * mat5HQprice);
var mat6Cost = (mat6NQ * mat6NQprice) + (mat6HQ * mat6HQprice);
var crystal1Cost = (crystal1Qty * crystal1Price);
var crystal2Cost = (crystal2Qty * crystal2Price);
var total = mat1Cost +
mat1Cost +
mat1Cost +
mat1Cost +
mat1Cost +
mat1Cost +
crystal1Cost +
crystal2Cost;
document.getElementById('totalCost').value = mat1Cost;
return false;
}
I'm new to javascript, and thought declaring the variables right after would make them accessible to any function that I would need to use them in, but they are undefined in the calculate function. If I declare them in calculate() it's fine, so is this a scope problem?
Thanks in advance for any help!
The problem is you're grabbing the elements values before you've assigned anything to them. You're getting a copy, not a reference.
var initialValue = document.getElementById('text').value;
function clickHandler() {
// Notice how I have to grab the value again when I want an updated value
var updatedValue = document.getElementById('text').value;
console.log('Initial:', initialValue);
console.log('Updated:', updatedValue);
}
<input id="text" />
<button onclick="clickHandler()">Click Me</button>
#epascarello has commented the real answer, there is no much science there, this is javascript, therefore is a script language, so as soon as this file is loaded, those variables are filled with the initial data of each field, so, if you want to get the value at the point you press calculate(), you should get your variables.