I am trying to create a function for calculating a pre-set commission within google sheets. I keep on getting a syntax error for line 15.
Can anyone help me? there is a description of what I am trying to achieve commented on the top.
Thanks.
/**Calculates commission based on the following rules
if less than 33 subtract 3
if in between 33.01 and 55 subtract 5
if in between 55.01 and 100 subtract 10
if more than or equal to 100 multiply by .9
*/
function comS(input) {
if (input <= 33) {
return (input - 3);
}
else if (input >= 33.01 && <= 55) {
return (input - 5);
}
else if (input >= 55.01 && <= 100) {
return (input * 0.9);
} else {
return "Please insert a valid amount"
}
}
/**
End of function
*/
That's not how && operators work, you need to set the comparison on each side:
(input >= 55.01 && input <= 100)
Also, take a moment to look on how to make a good question on SO.
https://stackoverflow.com/help/how-to-ask
Related
I'm trying to figure out what my options are here when I need to use a column number in a formula, and if I really need to write a column number to column letter method to accomplish what I'm trying to do.
See this method I have here:
createFormulas(lookupField, lookupColumns) {
// Iterate through the lookupColumn array
lookupColumns.forEach(value => {
let columnNumber = this.getColumn(this.headers, value);
let range = this.sheet.getRange(2, columnNumber, this.lastRow - 1, 1);
// range.setFormula('=$A2');
range.setFormula('=' + columnNumber + '2' ); // doesn't work obviously
})
}
I'm trying to add formulas in a column based on the column.
this.getColumn() returns the column number based on the column name being passed in.
let range sets the range I want to set the formula in
range.setFormula('=$A2') pastes this formula into range and updates the reference accordingly (i.e., $A3, $A4, etc.). This isn't the formula I ultimately want to use, just a simplified example.
I need to set the column in the reference dynamically, however.
What I have obviously won't work: range.setFormula('=' + columnNumber + '2' );. That would just result in something like 72 where 7 is the column number.
I know I can write a method that will convert the column number into a letter. I'm just surprised there isn't a built in method for doing that or some other native way of accomplishing this.
For example, in Excel VBA I think you can do something like "=" & Cells(2, columnNumber).Address or something like that (been a while, I could be wrong), which should equate to =A2, =A3, =A4, etc. in the range.
So before writing this column number to letter method, I just wanted to check: is that the only way to accomplish what I'm after or is there a native way of handling this that I'm just not seeing?
Actually, was able to do this using .getA1Notation().
Refactored to the following and it works as expected:
createFormulas(lookupField, lookupColumns) {
// Iterate through the lookupColumn array
lookupColumns.forEach(value => {
let columnNumber = this.getColumn(this.headers, value);
let formulaRange = this.sheet.getRange(2, columnNumber, this.lastRow - 1, 1);
let referenceRange = this.sheet.getRange(2, this.idColumn, this.lastRow - 1, 1);
formulaRange.setFormula("=" + referenceRange.getCell(1, 1).getA1Notation());
})
}
Column To Letters
I followed Yuri's path to the numbers to letter functions and I'm a bit baffled that we have forgotten that there are 26 letters in the alphabet and so after looking at the various functions at that reference none of them seem to have worked for me. So here's my replacement:
function colToletters(num) {
let a = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (num < 27) return a[num % a.length];
if (num > 26) {
num--;
let letters = '';
while (num >= 0) {
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[num % 26] + letters;
num = Math.floor(num / 26) - 1;
}
return letters;
}
}
This will calculate the column letters for 1 to 1000 and I check all the way to 703 where the letters go to AAA and they look good all the way.
Just in case. Based on https://stackoverflow.com/a/64456745/14265469
function numberToLetters(num) {
// num--; // if you need 1 --> A, 2 --> B, 26 --> Z
let letters = '';
while (num >= 0) {
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[num % 26] + letters;
num = (num - num % 26) / 26 - 1;
}
return letters;
}
console.log(numberToLetters(0)); // --> A
console.log(numberToLetters(25)); // --> Z
console.log(numberToLetters(26)); // --> AA
On my Codeigniter project, I would like to allow user to submit data into database only 3 times in a day (that user can submit on another day). Have anyone know the better way to do this?
Sorry for my bad English and duplicated question.
field
[create_perday], [timestamp_update], [reset_times]
create_perday - times of update [ex. value = 1, 2, 3]
reset_times - is boolean [0 = not reset, 1 = reset]
sudo code :
if(date(timestamp_update) > date(now) ) { // check date
if(!reset_times) {
doReset[times]
}
if(create_perday < 3) { // check times
create_perday = create_perday + 1;
return true;
} else {
return false;;
}
}
in the following code script for google Spreadsheets, I tried to make a program in which two pieces of information would be inputted to return a desired value that depends on BOTH values. Say, getValcharge ("OptionA", 2000) would return "76", or getValcharge ("OptionB",6000) would return 70. However, it seems to me that I keep getting returned the very last value possible: getValcharge("OptionA"/"OptionB"/"OptionC",1000) would return me "30". Even if I were to put an "OptionD" for the value, it would return "30" if the second number is under 5001.
Thus, it seems to only follow the second parameter --and thus only the second--even when closed off and is supposed to be not accessible to the first.
I am new to Script editor but do have modest Java experience (it'd work were this Java..) Could someone offer any advice/fixes? Any is appreciated. Thanks.
function getValcharge (valType, valAmount) {
var valcost =0;
if(valType="OptionA"){
if(valAmount < 5001)
{valcost = 76;}
if(valAmount > 5000 && valAmount <10001)
{valcost = 113;}
}
if(valType="OptionB"){
if(valAmount < 5001)
{valcost=43; }
if(valAmount > 5000 && valAmount <10001)
{valcost = 70;}
}
if(valType="OptionC")
{
if(valAmount < 5001)
{ valcost = 30; }
if(valAmount > 5000 && valAmount <10001)
{ valcost = 46; }
}
return valcost;
}
In Javascript you need to use a double-equals sign to test for equivalence, eg:
if(valType=="OptionA"){
What's the simplest way in actionscript to find if number is between -20 and +20, and return a true/false? I can see there's a number validator but I see it involves firing and catching events, which I think maybe overkill for the simple test I'm trying to do here.
Simplest way would be comparing the number with both values and logical combine the results:
return num > -20 && num < 20;
You may use >= or <= to include the values if needed.
You can make that into a nice function:
function isBetween(num:Number, lowerBound:Number, upperBound:Number):Boolean {
return num > lowerBound && num < upperBound;
}
Just write a function, conceptually like this:
protected function validatateNumbers(value:Number):Boolean{
if((value > -20) && (value <20)){
return true;
}
return false;
}
Then call the function whenever you want to validate your input.
I have two fields
- amount (decimal (11, 2))
- gift_amount (decimal (11, 2))
When I do an update on either for a value equal to or below 999.99, it saves correctly.
However, if I go over that, then it drops the value right back to down 1 - 10.
Is this a known issue or am I going wrong using decimal?
Heres some PHP code of what I'm doing just to make it clearer (although I'm 100% its not the PHP's fault.
if ($total_balance >= $cost) {
if ($this->user->balance->gift_amount > 0) {
$total_to_be_paid = number_format($cost, 2) - number_format($this->user->balance->gift_amount, 2);//figure out how much is left after the gift total
$this->user->balance->gift_amount -= number_format($cost, 2); //deduct from the gift balance
$this->user->balance->gift_amount = (number_format($this->user->balance->gift_amount, 2) < 0) ? number_format(00.00, 2) : number_format($this->user->balance->gift_amount, 2); //if the gift balance went below 0, lets set it to 0
if ($total_to_be_paid > 0) {
$this->user->balance->amount = number_format($this->user->balance->amount, 2) - number_format($total_to_be_paid, 2);
}
} else {
$this->user->balance->amount = number_format($this->user->balance->amount, 2) - number_format($cost, 2);
}
if ($object = Model_ClipBought::create(array('clip_id' => $clip->id, 'user_id' => $this->user->id, 'currency_name' => $user_currency, 'cost' => $cost, 'downloads' => $clip->downloads, 'expires' => time() + ($clip->expires * 86400)))) {
$this->user->balance->save();
$download = new Model_Download(ROOT_PATH."/public/files/Clip/$clip->file_url");
$download->execute();
} else {
throw new exception('We could not finish the purchase, this has been reported, sorry for the inconvenience.');
}
} else {
throw new exception('You dont have enough money in your account todo this');
}
exit;
}
You should not be using number_format until it's time to actually output/display the number to the user. It inserts your system's default thousand seperator (a comma by default):
number_format(999.99, 2) -> 999.99
number_format(1234.56, 2) -> 1,234.56
If you use these values in subsequent calculations in PHP, or try to insert verbatim into MySQL, you'll get funky values:
2345.67 + 1.0 = 2346.67
but using number_format() gives you this parsing sequence:
number_format(2345.67) + 1.0 -> "2,345.67" + 1.0
"2,345.67" + 1.0 -> "2" + 1.0
2 + 1.0 -> 3
Notice how "2,345.67" was truncated down to just "2" - the comma turns your nice number into a string, and now you're bound by string->integer parsing rules, which drops everything after the first non-numeric character in the string.
If you're trying to keep everything down to 2 decimal places throughout the calculation sequence, consider using sprintf('%0.2f', $value) instead.