can anyone help me how to proper use tofixed function in my script because im kinda new about it and when i put the tofixed my script wont work...help me please.
current script:
<script>
function optTotal1()
{
var a1 = document.querySelector('select[name="optA1"]');
var b1 = document.querySelector('select[name="optB1"]');
var c1 = document.querySelector('select[name="optC1"]');
var d1 = document.querySelector('select[name="optD1"]');
var e1 = document.querySelector('select[name="optE1"]');
if (a1.value && a1.value != "")
a1 = parseFloat(a1.value);
else
a1 = 0;
if (b1.value && b1.value != "")
b1 = parseFloat(b1.value);
else
b1 = 0;
if (c1.value && c1.value != "")
c1 = parseFloat(c1.value);
else
c1 = 0;
if (d1.value && d1.value != "")
d1 = parseFloat(d1.value);
else
d1 = 0;
if (e1.value && e1.value != "")
e1 = parseFloat(e1.value);
else
e1 = 0;
document.getElementById("total1").value.toFixed(2) = parseFloat(a1)+parseFloat(b1)+parseFloat(c1)+parseFloat(d1)+parseFloat(e1);
}
</script>
Wrap all the values with bracket then use it after you've calculated the value
document.getElementById("total1").value=(parseFloat(a1)+parseFloat(b1)+parseFloat(c1)+parseFloat(d1)+parseFloat(e1)).toFixed(2);
Related
I am still a newbie in this, but I want to run Onedit function script on google spreadsheet in a way that I will only get value on column E if the value on column D is "completed" and at the same time I will get value on column F depending on the value on B1 ( and that's only if the column D is"completed"). here is my spreadsheet: https://docs.google.com/spreadsheets/d/1-xLH3mb0fGngocOleCNExEmP6FnDMEFg2uLFXC10XEk/edit#gid=0
function onEdit(e){
var s = SpreadsheetApp.getActiveSpreadsheet();
if( s.getName() == "Hub" ){
var r = s.getActiveCell();
if (r.getColumn() == 4) {
var timecell = r.offSet(0, 1);
var assldapv = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Hub").getRange("B1").getValue();
var assldapo = r.offset(0, 2);
if ( r.getValue() === 'completed' ) {
timecell.setValue(new Date());
assldapo.setValue(assldapv); }
However this function is not working, so can you please give me a template on how to proceed with such script.
Try this
function onEdit(e) {
var sh = e.range.getSheet();
if (sh.getName() == "Hub" && e.range.columnStart == 4 && e.value == "completed") {
var timecell = e.range.offSet(0, 1);
var assldapv = sh.getRange("B1").getValue();
var assldapo = e.range.offset(0, 2);
timecell.setValue(new Date());
assldapo.setValue(assldapv);
}
}
I am trying to move cursor one more position to the right in input.
var start = this.selectionStart,
end = this.selectionEnd;
this.setSelectionRange(start, end);
This works for keeping position in the same place after input value changed, but iam trying to add one more position to it, because my format code add "-" character after 4 chars.
$("input[name=login-access-token]").keypress(function(event){
var start = this.selectionStart,
end = this.selectionEnd;
console.log(start);
var input = $(this).val();
input = input.replace(/[\W\s\._\-]+/g, '');
var split = 4;
var chunk = [];
for (var i = 0, len = input.length; i < len; i += split) {
split = 4;
chunk.push( input.substr( i, split ) );
}
var test = [];
chunk.forEach(element => {
test.push(element.match(/.{1,4}/g));
});
test.forEach(element => {
if(element.length!=4){
$(this).val(function() {
var out = chunk.join("-").toUpperCase();
if(out.length == 4 && event.keyCode != 8){
out += "-";
}
if(out.length == 9 && event.keyCode != 8 ){
out += "-";
}
return out;
});
}
});
this.setSelectionRange(start, end);
var selection = window.getSelection();
selection.addRange(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<input type="text" name="login-access-token" placeholder="Code" />
var selection = window.getSelection();
selection.addRange(1); //not working :/
I solved it! Just add +1 to start and end which is values what we get from selectionStart.
if(out.length == 4 && event.keyCode != 8){
out += "-";
start = start+1;
end = end +1;
}
if(out.length == 9 && event.keyCode != 8 ){
out += "-";
start = start+1;
end = end +1;
}
I have a form that I'm using to calculate some numbers, and the final 3 input fields on the form are disabled because they show the results of the calculator.
I'm using the following javascript/jquery to add commas to the user editable fields which works great but I can't seem to find a way to add commas to the "results" fields:
$('input.seperator').change(function(event){
// skip for arrow keys
if(event.which >= 37 && event.which <= 40){
event.preventDefault();
}
var $this = $(this);
var num = $this.val().replace(/,/gi, "").split("").reverse().join("");
var num2 = RemoveRougeChar(num.replace(/(.{3})/g,"$1,").split("").reverse().join(""));
// the following line has been simplified. Revision history contains original.
$this.val(num2);
});
function RemoveRougeChar(convertString){
if(convertString.substring(0,1) == ","){
return convertString.substring(1, convertString.length)
}
return convertString;
}
This is what I'm using the populate the fields, basically the fields show the results in dollars, so I'm trying to add a comma every 3 numbers:
$('#incorrect-payment').val(fieldK);
$('#correcting-payment').val(fieldL);
$('#total-cost').val(fieldM);
I think you'd want to use a function like this:
function FormatCurrency(amount, showDecimals) {
if (showDecimals == null)
showDecimals = true;
var i = parseFloat(amount);
if (isNaN(i)) { i = 0.00; }
var minus = false;
if (i < 0) { minus = true; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if (showDecimals) {
if (s.indexOf('.') < 0) { s += '.00'; }
if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
}
//s = minus + s;
s = '$' + FormatCommas(s, showDecimals);
if (minus)
s = "(" + s + ")";
return s;
}
function FormatCommas(amount, showDecimals) {
if (showDecimals == null)
showDecimals = true;
var delimiter = ","; // replace comma if desired
var a = amount.split('.', 2)
var d = a[1];
var i = parseInt(a[0]);
if (isNaN(i)) { return ''; }
var minus = '';
if (i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while (n.length > 3) {
var nn = n.substr(n.length - 3);
a.unshift(nn);
n = n.substr(0, n.length - 3);
}
if (n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if (!showDecimals) {
amount = n;
}
else {
if (d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
}
amount = minus + amount;
return amount;
}
May be you might want to trigger change event manually through javascript for your three read-only input fields. Using jquery trigger . I am not sure but it seems like a bad idea to have a read-only input field if no user can change these values. Usually having read-only input fields is good if a user with some security can edit those and some cannot.
i got a little problem with my code: i got 3 pieces of code, but i can't combine it together.
All three codes put timestamps in different cells when certain cells change.
If I change cell A1, then in cell B1 insert timestamp. If the change in cell A2, then in cell B2 insert timestamp , etc.
Can you help me? Thanks.
1)
function onEdit(event) {
var tsheet = 'Заявки' ;
var lcol = 17;
var rcol = 17;
var tcol = 18;
var s = event.source.getActiveSheet();
var sname = s.getName();
if (sname == tsheet) {
var r = event.source.getActiveRange();
var scol = r.getColumn();
if (scol >= lcol && scol <= rcol) {
s.getRange(r.getRow(), tcol).setValue(new Date());
}
}
}
2)
function onEdit(event) {
var tsheet = 'Заявки' ;
var lcol = 15;
var rcol = 15;
var tcol = 16;
var s = event.source.getActiveSheet();
var sname = s.getName();
if (sname == tsheet) {
var r = event.source.getActiveRange();
var scol = r.getColumn();
if (scol >= lcol && scol <= rcol) {
s.getRange(r.getRow(), tcol).setValue(new Date());
}
}
}
3)
function onEdit(event) {
var tsheet = 'Заявки' ;
var lcol = 9;
var rcol = 9;
var tcol = 8;
var s = event.source.getActiveSheet();
var sname = s.getName();
if (sname == tsheet) {
var r = event.source.getActiveRange();
var scol = r.getColumn();
if (scol >= lcol && scol <= rcol) {
s.getRange(r.getRow(), tcol).setValue(new Date());
}
}
}
Put the column specification in an array:
function onEdit(event) {
var tsheet = 'Заявки' ;
var colspec=[[17,17,18],[15,15,16],[9,9,8]];
var s = event.source.getActiveSheet();
var sname = s.getName();
if (sname == tsheet) {
var r = event.source.getActiveRange();
var scol = r.getColumn();
var i=colspec.length;
while (i>0) {
i--;
if (scol >= colspec[i][0] && scol <= colspec[i][1]) {
s.getRange(r.getRow(), colspec[i][2]).setValue(new Date());
}
}
}
}
I was told I should consolidate my if statements. I'm not sure how to do this? Also, is there anything else wrong in this script? It is for a google doc script.
function onEdit(e) {
var colorA = "yellow";
var colorB = "#dddddd";
var colorC = "#dddddd";
var sheet = e.source.getActiveSheet();
var range = e.source.getActiveRange();
// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
sheet.insertRowAfter(range.getRow());
var r = range.getRow() + 1;
sheet.getRange("A" + r + ":H" + r).setBackgroundColor(colorC);
}
}
if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
var rows = sheet.getMaxRows();
//two ranges
//column C
var rangeC = sheet.getRange("C1:C"+rows);
var valuesC = rangeC.getValues();
//column H range
var rangeH = sheet.getRange("H1:H"+rows);
var colorH = rangeH.getBackgroundColors();
var valuesH = rangeH.getValues();
//iterate over each row in column C and H
//then change color
for (var row = 0; row < valuesC.length; row++) {
//check for columnC and column H
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
}
sheet.getRange("H1:H" + rows).setBackgroundColors(colorH);
}
}
Here is the other one
ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "New PO", functionName: "NewPO"}];
ss.addMenu("New PO", menuEntries);
}
function NewPO() {
SpreadsheetApp.getActiveSheet().insertRowsBefore(1,6);
// Adjust this range accordingly, these are the cells that will be
// copied. Format is getRange(startRow, startCol, numRows, numCols)
ss.getSheetByName("PO Form").getRange(1, 1, 6, 8)
.copyTo(SpreadsheetApp.getActiveSheet().getRange(1, 1, 6, 8));
}
function onEdit(e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
// 1 is A, 2 is B, ... 8 is H
if (r.getColumn() == 8 && r.getValue() == "x") {
r.setValue(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));
}
}
Besides what murray noted, there are several instances where you repeat the same expression:
if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
could be:
var col = e.source.getActiveRange().getColumn();
if(col == 3 || col == 8) {
This applies to a lesser extent to:
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
which could be (for instance):
var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
hRow[0] = colorA;
} else if (valuesH[row][0] != "") {
hRow[0] = colorB;
}
only thing i can see:
// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
// 3 is column C
if (range.getColumn() == 3 && range.getValue() != "") {