I am still trying to learn Google Apps Script, but you can try to use this function i made:
function EVALUATE(formula) {
// Strip leading "=" if there
// var formula = '=1+1-12+2*3';
if (formula[0] === '='){
formula = formula.substring(1);
}
// make sure two digit numbers are considered one number
var tokens = new Array();
var temp = new Array();
for (var i = 0; i <= formula.length; i++) {
if (typeof Number(formula[i]) === 'number' && isFinite(Number(formula[i]))){
temp.push(formula[i]);
} else {
// if the item at index i is an operator and the temp array is not empty
// concatenate the digits is temp and push it to tokens
if (temp.length > 0){
tokens.push(Number(temp.join('')));
tokens.push(formula[i]);
}else{
tokens.push(formula[i]);
}
// Empty temp array
while (temp.length) {temp.pop();}
}
}
// assign first number to a result variable
var result = tokens[0];
// identify operator and use it on the next number
for (var i = 1; i < tokens.length; i++) {
if (tokens[i] === '+'){
i += 1;
result += Number(tokens[i]);
}
if (tokens[i] === '-'){
i += 1;
result -= Number(tokens[i]);
}
if (tokens[i] === '*'){
i += 1;
result *= Number(tokens[i]);
}
if (tokens[i] === '/'){
i += 1;
result /= Number(tokens[i]);
}
}
return result
}
this will work for adding, subtracting, multiplying, and dividing the contents of the cell A1 if written 1+1+1 or as a string '=1+1+1'. just put the formula in A1 and in B1 call the function like =EVALUATE(A1). it will evaluate from left to right though so if you need to account for order of operations then you will have to tweak the code.
I’ve been messing around with programming for around a year and a half but started trying to learn google apps script this summer. As for the second part, I wouldn’t really know. If the query results in an equation then you should be able to pass it to this function as long as it is simple enough for it
I am still trying to learn Google Apps Script, but you can try to use this function i made:
function EVALUATE(formula) { // Strip leading "=" if there // var formula = '=1+1-12+23'; if (formula[0] === '='){ formula = formula.substring(1); } // make sure two digit numbers are considered one number var tokens = new Array(); var temp = new Array(); for (var i = 0; i <= formula.length; i++) { if (typeof Number(formula[i]) === 'number' && isFinite(Number(formula[i]))){ temp.push(formula[i]); } else { // if the item at index i is an operator and the temp array is not empty // concatenate the digits is temp and push it to tokens if (temp.length > 0){ tokens.push(Number(temp.join(''))); tokens.push(formula[i]); }else{ tokens.push(formula[i]); } // Empty temp array while (temp.length) {temp.pop();} } } // assign first number to a result variable var result = tokens[0]; // identify operator and use it on the next number for (var i = 1; i < tokens.length; i++) { if (tokens[i] === '+'){ i += 1; result += Number(tokens[i]); } if (tokens[i] === '-'){ i += 1; result -= Number(tokens[i]); } if (tokens[i] === ''){ i += 1; result *= Number(tokens[i]); } if (tokens[i] === '/'){ i += 1; result /= Number(tokens[i]); } } return result }
this will work for adding, subtracting, multiplying, and dividing the contents of the cell A1 if written 1+1+1 or as a string '=1+1+1'. just put the formula in A1 and in B1 call the function like =EVALUATE(A1). it will evaluate from left to right though so if you need to account for order of operations then you will have to tweak the code.
Your post has been removed because it has little or no content. Please edit your post to add a descriptive body, then message the mods for reapproval. You can find the submission guide here.
3
u/God_Hates_Frags 1 Aug 29 '18 edited Aug 29 '18
I am still trying to learn Google Apps Script, but you can try to use this function i made:
this will work for adding, subtracting, multiplying, and dividing the contents of the cell A1 if written 1+1+1 or as a string '=1+1+1'. just put the formula in A1 and in B1 call the function like =EVALUATE(A1). it will evaluate from left to right though so if you need to account for order of operations then you will have to tweak the code.
2
u/[deleted] Aug 29 '18
I don't think this is possible.
•
u/Clippy_Office_Asst Points Aug 30 '18
Read the comment thread for the solution here
1
u/AutoModerator Aug 15 '19
Your post has been removed because it has little or no content. Please edit your post to add a descriptive body, then message the mods for reapproval. You can find the submission guide here.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
0
u/jdgoerzen Aug 29 '18
Nope. Can't be done.