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);
}}
Related
i wrote a code that should take a value from a cell and than convert it to the string and than into the array. its working fine. I can see the value of arr in Logs as an array with the input of the cell. For example, in the cell are " dog, cat " and tha arr value in Logs is [dog, cat].
But after i create this array, i would like to make a loop on it. And than i become in Logs, that arr is undefined. Can somebody help me please? im working on it for 2 days :(
Here is my code:
function animal (s,z){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getRange("SM");
var columnNumber = getColumnNumberOfSM(s);
var rowNumber = getRowNumberOfSM(z);
var colAction = columnNumber + 1;
var action = sheet.getRange(rowNumber, colAction, 1, 1).getValues();
var bar = action.toString();
var arr = [{}];
arr = bar.split(", ");
//return arr; // returns an array [dog, cat]
var foo = arr; // underfined
for (var i = 0; i <= foo.length; ++i) {
if (foo[i] == "dog") {
Logger.log(upload());
}
}
}
now i edit my code and its working fine,but only with "dog" but not with "cat"
function animal(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var action = sheet.getRange("C3").getValue();
var bar = action.toString();
var arr = bar.split(", ");
for (var i = 0; i <= arr.length; ++i) {
if (arr[i] == "dog") { // works
Logger.log(upload());
}
if (arr[i] == "cat") { // doesn't work
Logger.log(upload());
}
}
}
You don't need to initiate arr as blank you can directly initialize it as a result to split method , also one point I didn't get is why do you need to assign arr to foo? You can directly iterate through arr.length and perform the required operation.
Here's my working snippet.
var parts = path.split(",");
for (var i = 0; i < parts.length; i++) {
if (parts[i] == 'dog'){
Logger.log(parts[i]);
}
}
I'm using a gfx as a button and I want it to set the values from the array to a set of rows and columns. So far this is what I have.
function loadTrades(){
var balancesSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Balances");
var exchange = balancesSheet.getRange("D1").getValue();
var trades = Gettrades(exchange);
var arr = [];
var c = [];
for (var i=0;i < trades.length-1;i++) {
c=[];
for (var j=0; j< trades[0].length;j++){
c.push(trades[i][j]);
}
arr.push(c);
}
//var arr = json2array(trades); this does the same as above but in a funtion of its own.
var destinationRange = balancesSheet.getRange("D2");
destinationRange.setValues(arr);
};
function json2array(data){
var results = [];
var keys = [];
var values = [];
for (var i in data){
for (var Key in data[i]){
if (i == 0) keys.push(Key);
values.push(data[i][Key]);
}
if (i == 0){
results.push(keys);
keys = [];
}
results.push(values);
values = [];
}
return results;
};
There is up to 500 rows in the array, and 7 columns.
I get the error.
Incorrect range height, was 3 but should be 1.
I think I have a miss understanding of how getRange and setValues work in google sheets.
I've read through this site and a few other examples here on stackoverflow, but can't find my solution.
As the error occurs on
destinationRange.setValues(arr);
the origin of the problem is on the previous line
var destinationRange = balancesSheet.getRange("D2");
The above because balancesSheet.getRange("D2") returns a single cell but arr, according to the error, has a height of 3.
One way to solve this is replace
var destinationRange = balancesSheet.getRange("D2");
by
var destinationRange = balancesSheet.getRange(2,4,arr.length,arr[0].length);
Reference
getRange(row, column, numRows, numColumns)
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();"
I'm trying to fetch my ETH balance and transactions from etherscan.io website and I'm trying to use the same code I used before for another website. But it seems returning me an empty array, and also the error "The coordinates or dimensions of the range are invalid."
This is the code:
function getTx() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var url = 'http://api.etherscan.io/api?module=account&action=txlist&address=0x49E81AA0cFE7eeA9738430212DC6677acF2f01a1&sort=asc';
var json = UrlFetchApp.fetch(url).getContentText();
var data = JSON.parse(json);
var rows = [],
array;
for (i = 0; i < data.length; i++) {
array = data[i];
rows.push([array.timeStamp, array.from, array.to, array.value]);
}
Logger.log(rows)
askRange = sheet.getRange(3, 1, rows.length, 3);
askRange.setValues(rows);
}
The logged "rows" is empty, what am I doing wrong?
Thank you
How about a following modification?
Modification points :
There is data you want at data.result.
Number of columns of data is 4.
Modified script :
function getTx() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var url = 'http://api.etherscan.io/api?module=account&action=txlist&address=0x49E81AA0cFE7eeA9738430212DC6677acF2f01a1&sort=asc';
var json = UrlFetchApp.fetch(url).getContentText();
var data = JSON.parse(json);
var rows = [],
array;
for (i = 0; i < data.result.length; i++) { // Modified
array = data.result[i];
rows.push([array.timeStamp, array.from, array.to, array.value]);
}
Logger.log(rows)
askRange = sheet.getRange(3, 1, rows.length, 4); // Modified
askRange.setValues(rows);
}
If I misunderstand your question, I'm sorry.
I wish to get a couple of properties of all the folders in my GDrive and write these properties to a spreadsheet. Because of the large number of folders (over 300) I have decided to use Paging and Batch processing. This seems to be working but I can't seem to write the Array[][] I've created in the batch processing to the spreadsheet.
I'm am getting the following error when I try to set the values in my spreadsheet:
Cannot convert (class)#3cc8188e to Object[][].
I did not find any listed questions that were similar to my problem.
The last line of the script is highlighted when the error appears. Code is:
function myFunction() {
var folder = DocsList.getFolder('MyFolder');
var subfolders = null;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getRange('a1');
var x = 0;
var pageSize = 250;
var token = null;
var xfolders = new Array(500);
do {
var resultset = folder.getFoldersForPaging(pageSize, token);
subfolders = resultset.getFolders();
token = resultset.getToken();
x = subfolders.length;
for (var a = 0; a < subfolders.length; a++) {
var contents = subfolders[a].getFiles();
xfolders[a] = new Array(6);
if(contents.length>0) {
xfolders[a][0] = subfolders[a].getName();
xfolders[a][1] = subfolders[a].getDateCreated();
xfolders[a][2] = subfolders[a].getLastUpdated();
xfolders[a][3] = contents.length;
xfolders[a][4] = subfolders[a].getSize();
xfolders[a][5] = a;
}
}
} while (subfolders.length > pageSize)
sheet.getRange(1, 1, x, 6).setValues(xfolders);
}
You've started with an array xfolders for which you've initialized a length. There's no need to do this - it's enough to know that it's an array. Later you call getRange() with rows==x, where x = subfolders.length... and if that isn't 500 (the length of xfolders) you will end up with your error. (...because you have undefined array elements.)
What you need to do is make sure that the range you're writing to has the same dimensions as the values you're presenting. One way to do that is to calculate the range dimensions using xfolders itself.
Here's your function, refactored to grow xfolders dynamically, and then to use its dimensions for output to the spreadsheet.
function myFunction() {
var folder = DocsList.getFolder('MyFolder');
var subfolders = null;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getRange('a1');
var pageSize = 250;
var token = null;
var xfolders = [];
do {
var resultset = folder.getFoldersForPaging(pageSize, token);
subfolders = resultset.getFolders();
token = resultset.getToken();
for (var a in subfolders) {
var contents = subfolders[a].getFiles();
xfolders[a] = [];
if(contents.length>0) {
xfolders[a][0] = subfolders[a].getName();
xfolders[a][1] = subfolders[a].getDateCreated();
xfolders[a][2] = subfolders[a].getLastUpdated();
xfolders[a][3] = contents.length;
xfolders[a][4] = subfolders[a].getSize();
xfolders[a][5] = a;
}
}
} while (subfolders.length == pageSize) // Quit when we aren't getting enough folders
sheet.getRange(1, 1, xfolders.length, xfolders[0].length).setValues(xfolders);
}