Google sheets code for arrays - arrays

I'm trying to get a code to work to calcualte average true range. So in the spreadsheet side I have
=getATR(GoogleFinance("stock", ʺallʺ, today()-60,today()))
And the code for the app is below. However I keep getting this TypeError: cannot set property "0.0" of undefined to "7.1". The line with the problem is the first instance where trurange[i][0] is called.
Appreciate your help with this, seems something silly on the syntax side.
function getATR(arr) {
var atr = 0;
var truerange = [];
var i=39;
for (var row = arr.length-1; row >= arr.length-40; row--) {
truerange[i][0] = arr[row][3] - arr[row][4];
truerange[i][1] = Math.abs( arr[row][3] - arr[row-1][2] );
truerange[i][2] = Math.abs( arr[row][4] - arr[row-1][2] );
truerange[i][3]= Math.max(truerange[i][0],truerange[i][1],truerange[i][2]);
i=i-1;
}
for(var row2 = 1; row2<40; row2++){
truerange[row2][4] = (truerange[row2-1][4]*13+truerange[row2][3])/14;
if(row2=39) {atr = truerange[row2][4]}
}
return atr;
}
Browser: ie9
Operating System: win7
Drive on the Web / Drive for desktop: Web

I am looking for a 6 day SMA of ATR. This code works for me
function getATR(arr) {
arr[1][9] = Math.abs( arr[1][2] - arr[1][3] )
for (var row = 2; row < 41; row++)
{
arr[row][6] = arr[row][2] - arr[row][3];
arr[row][7] = Math.abs( arr[row][2] - arr[row-1][4] );
arr[row][8] = Math.abs( arr[row][3] - arr[row-1][4] );
arr[row][9] = Math.max(arr[row][6],arr[row][7],arr[row][8]);
if (row>6) { arr[row][10] = (arr[row-5][9]+arr[row-4][9]+arr[row-3][9]+arr[row-2][9]+arr[row-1][9]+arr[row][9])/6 }
}
return arr[40][10];
}

Related

Google Script - Populate google Forms multipleChoiceItem AND pageBreak with google sheet's values

I try to populate a google forms multipleChoiceItem with a lot of Item from my google sheet AND also configure the pagebreak (conditionnal answers) from my google sheet
EDIT: There Is my code, Hope it is easier to understand my problem
function AutoPopulateGF() {
var form = FormApp.openById("1fyej6DSh2B1spsurOR-gnD5s1NWrekt4Sf8xiIL7EjY");
var items = form.getItems();
var ss = SpreadsheetApp.openById("1hEBCEvUqJHnRuRqZS0FmhOZhOKG4PMa83czvdn01qcM").getSheetByName("Remplir ICI")
var data = ss.getDataRange().getValues();
var numrows = ss.getDataRange().getLastRow()
for (var i in items) {
if (items[i].getTitle() == 'Choisissez le module ') {
/*
//THIS BELOW IS NOT WORKING
items[i].asMultipleChoiceItem().setChoices([
for (var j = 0; j < numrows; ++j) {
var column = data[j];
if (column[0] != "") {
items[i].asMultipleChoiceItem().createChoice(column[0],form.getItemById(column[30]).asPageBreakItem()),
}
}
])
*/
//THIS WORKS BUT VERY TIME CONSUMMING... (I HAVE MORE THAN 200 LINES)
items[i].asMultipleChoiceItem().setChoices([
items[i].asMultipleChoiceItem().createChoice(ss.getRange("A70").getValues(),form.getItemById(ss.getRange("AD70").getValues()).asPageBreakItem()),
items[i].asMultipleChoiceItem().createChoice(ss.getRange("A71").getValues(),form.getItemById(ss.getRange("AD71").getValues()).asPageBreakItem()),
items[i].asMultipleChoiceItem().createChoice(ss.getRange("A72").getValues(),form.getItemById(ss.getRange("AD72").getValues()).asPageBreakItem()),
items[i].asMultipleChoiceItem().createChoice(ss.getRange("A73").getValues(),form.getItemById(ss.getRange("AD73").getValues()).asPageBreakItem()),
items[i].asMultipleChoiceItem().createChoice(ss.getRange("A74").getValues(),form.getItemById(ss.getRange("AD74").getValues()).asPageBreakItem())
])
}
}
}
How to Automate the process ?
Thanks for your help!

TypeError: Impossible to find getCell function in the Sheet object

I'm trying to get some data from a Google Sheet to insert in two arrays with a for loop but I think I'm using the wrong methods.
This is the code:
function regValori() {
var datAgg = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("datiaggiornati");
var f5 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foglio5");
var CValue = new Array();
var CName = new Array();
CValue.lenght = datAgg.getRange("B1").getValue();
CName.lenght = CValue.lenght;
for (var i = 0; i <= CValue.lenght; i++)
{
CValue[i] = datAgg.getCell(i, 16).getValue();
CName[i] = datAgg.getCell(i+2, 1).getValue();
}
var riga = f5.getRange("B2").getValue();
for (x = 3; x <= numCrypto; x++);
for (i=0;i<numCrypto;i++);
{
f5.getRange(riga, x).setValue(valCrypto[r+i]);
f5.getRange(2,numCrypto).setValue(nomiCrypto[r+i]);
}
}
The error:
TypeError: Impossible to find getCell function in the Sheet object
Thanks for your help!
Errors:
getCell() is a method of Range, not Sheet
Google Sheets starts from rowIndex = 1
If you don't need an offset (i.e. start from a certain cell), then you can use getRange:
for (var i = 0; i <= CValue.lenght; i++)
{
var r = i + 1; // rowIndex
CValue[i] = datAgg.getRange(r, 16).getValue();
CName[i] = datAgg.getRange(r+2, 1).getValue();
}
this is the solution:
function regValori() {
var datAgg = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("datiaggiornati");
var f5 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Foglio5");
var riga = f5.getRange(2, 2).getValue();
for (var i = 2; i <= 6; ++i) {
f5.getRange(riga, i + 1).setValue(datAgg.getRange("P" + i).getValue());
f5.getRange(2, i + 1).setValue(datAgg.getRange("A" + i).getValue());
}
}
i posted another question here for the same problem and I get a perfect answer how to use for loop to drastically reduce code lenght.
the only part I changed is this one: (var i = 2; i <= 6; ++i) with this one: (var i = 2; i <= n+1; ++i), so it can work perfectly with any variable n to get with command "var numCrypto = datAgg.getRange(1,2).getValue();"

Having an issue figuring out where to do calculations before using Upsert in mongodb

I'm using the angular-fullstack generator so there's 7 files in one endpoint (index, index.spec, orders.controller, orders.events, orders.integration, orders.model, and orders.socket). I'm not sure where to do computation to store in the fields when there is a PUT/Upsert. All the examples that I can google either use virtual fields or have generic code to do the computation. I know the computation I need to do but have no idea where to put it using this generator.
After a bit more searching this morning, I think what I want is to use getters/setters?
It's working in the controller as I presumed but i'm not sure if it's the best place to put these simple
function doCalcsSingle(res) {
var tOrderitems = 0;
var tRecitems = 0;
var tMissingitems = 0;
var today = new Date();
for(var i = 0; i < res.body.items.length; i++) {
res.body.items[i].missingItems = res.body.items[i].numOfOrdItems - res.body.items[i].numOfRecItems;
if(res.body.items[i].missingItems < 0 || !res.body.items[i].missingItems) {
res.body.items[i].missingItems = 0;
}
res.body.items[i].totalPrice = res.body.items[i].numOfOrdItems * res.body.items[i].unitPrice;
tOrderitems = tOrderitems + res.body.items[i].numOfOrdItems;
tRecitems = tRecitems + res.body.items[i].numOfRecItems;
tMissingitems = tMissingitems + res.body.items[i].missingItems;
if(tMissingitems < 0 || !tMissingitems) {
tMissingitems = 0;
}
}
res.body.totalOrdItems = tOrderitems;
res.body.totalRecItem = tRecitems;
res.body.totalItemsMissing = tMissingitems;
res.body.lastUpdated = today;
if(tMissingitems <= 0) {
res.body.activeOrder = false;
res.body.completedDate = today;
} else {
res.body.activeOrder = true;
}
return res;
}

Two Dimensional Array at Server-side Javascript on Xpages

need some help ..
Recently I've just created an array to calculate the value
function Calculate(){
for(x=1;x<7;++x){
var targetArr = [];
for(i=0;i<3;i++){
targetArr[i] = getComponent("product_"+x).getValue();
targetArr[i] = getComponent("quantity_"+x).getValue();
targetArr[i] = getComponent("stock_"+x).getValue();
}
var actualArr = [];
for(i=0;i<3;i++){
actualArr[i] = getComponent("actualProduct_"+x).getValue();
actualArr[i] = getComponent("actualQuantity_"+x).getValue();
actualArr[i] = getComponent("actualStock_"+x).getValue();
}
var achArr = [];
for(i=0;i<3;++i){
if((actualArr[i]>targetArr[i])||(targetArr[i]==0)||(targetArr[i]=="")||(targetArr[i]==null)){
achArr[i] = 1;
}else{
achArr[i] = (actualArr[i] / targetArr[i]);
}
if(isNaN(achArr[i])){
achArr[i] = 0;
}
x0 = achArr[i][x];
x1 = achArr[i][x];
x2 = achArr[i][x];
}
value = 100;
currentDocument.replaceItemValue("achProduct_"+x, x0*Value);
currentDocument.replaceItemValue("achQuantity_"+x, x1*Value);
currentDocument.replaceItemValue("achStock_"+x, x2*Value);
}}
but when i run the script, its got forever looping on my lotus domino server
and when i try to restart the web server its stucked and i have to force closed the server, and manually open it again
anyone can help me to solve my case ? i just need to display the achArr values
I'm trying to decipher your goal here. But first of all, your targetArr and actualArr for loops are wrong.
You'll end up with all values in targetArr equal to getComponent("stock_"+x).getValue()
Similarly, all values in actualArr equal to getComponent("actualStock_"+x).getValue()
And I don't think you even need a 2D array for achArr. I'm not entirely sure what you want to calculate in achArr's values, but I've written the code below. Is this closer to what you want?
function Calculate(){
for(var x=1;x<7;x++){
//Retrieve targetArr values
var targetArr = [];
targetArr[0] = getComponent("product_"+x).getValue();
targetArr[1] = getComponent("quantity_"+x).getValue();
targetArr[2] = getComponent("stock_"+x).getValue();
//Retrieve actualArr values
var actualArr = [];
actualArr[0] = getComponent("actualProduct_"+x).getValue();
actualArr[1] = getComponent("actualQuantity_"+x).getValue();
actualArr[2] = getComponent("actualStock_"+x).getValue();
//Generate achArr values
var achArr = [];
for(var i=0;i<3;i++){
if((actualArr[i]>targetArr[i])||(targetArr[i]==0)||(targetArr[i]=="")||(targetArr[i]==null)){
achArr[i] = 1;
}else{
achArr[i] = (actualArr[i] / targetArr[i]);
}
if(isNaN(achArr[i])){
achArr[i] = 0;
}
}
var value = 100;
currentDocument.replaceItemValue("achProduct_"+x, achArr[0]*value);
currentDocument.replaceItemValue("achQuantity_"+x, achArr[1]*value);
currentDocument.replaceItemValue("achStock_"+x, achArr[2]*value);
}}

strange server error GAS

I have a function running in google script. However it does not finish, it enters the main loop and then it stops somewhere, no errors. If i look in the execution description it says server error wait a while and try again.
I tried running it several times but it keeps stalling, but each time the last values of i and j are different from the previous stall.
I really can't imagine that this is a real server error, it must be the code.
function start() {
var sheet1 = SpreadsheetApp.openByUrl("xx").getSheetByName('Blad1');
var sheet2 = SpreadsheetApp.openByUrl("xx").getSheetByName('Blad1');
var range1 = sheet1.getRange(1,1,54,26);
var range2 = sheet2.getRange(1,1,10,7);
var teller = 0;
for(var i = 1; i<=range1.getNumRows(); i++){
for(var j = 1; j<=range2.getNumRows(); j++){
Logger.log(i);
Logger.log(j);
if(range1.getCell(i, 8).getValue() == range2.getCell(j, 2).getValue() && range1.getCell(i, 16).getValue() == range2.getCell(j, 4).getValue() && range1.getCell(i, 19).getValue() == range2.getCell(j, 6).getValue()){
range1.getCell(i,25).setValue(range2.getCell(j, 7).getValue());
range2.getCell(j,1).setValue("Script")
teller++;
}
}
}
Logger.log(teller);
};
I imagine you are hitting the SpreadsheetApp api too much. Best practice is to load the data you need into the script and iterate over it. Using your script as an example:
function start() {
var sheet1 = SpreadsheetApp.openByUrl("xx").getSheetByName('Blad1');
var sheet2 = SpreadsheetApp.openByUrl("xx").getSheetByName('Blad1');
var range1 = sheet1.getRange(1,1,54,26);
var range2 = sheet2.getRange(1,1,10,7);
var data1 = range1.getValues();
var data2 = range2.getValues();
var teller = 0;
for(var i = 0, r = data1.length; i < r; i++){
for(var j = 1, c = data2.length; j < c; j++){
Logger.log(i);
Logger.log(j);
if(data1[i, 8] == data2[j, 2] && data1[i, 16] == data2[j, 4] && data1[i, 19] == data2[j, 6]){
range1.getCell(i,25).setValue(data2[j, 7]);
range2.getCell(j,1).setValue("Script")
teller++;
}
}
}
Logger.log(teller);
};

Resources