Inserting Data Into a 2D Array Actionscript 3 - actionscript-3

Basically I'm trying to insert a number into a 2D array like this:
arrayName[X][Y] = 4;
but I get an error that says:
TypeError: Error #1010: A term is undefined and has no properties.
so how do I fix this or do it properly?

var arrFirst:Array = [];
var arrSecond:Array = [];
arrSecond[Y] = 4;
arrFirst[X] = arrSecond;

Related

JSON import - Cannot read property 'length' of undefined

I'm trying to import JSON via API into Google sheets but get the error
Cannot read property 'length' of undefined
Here is my code
function importRank(){
url = 'https://public-api.solscan.io/token/holders?tokenAddress=sinjBMHhAuvywW3o87uXHswuRXb3c7TfqgAdocedtDj&offset=0&limit=max'
var json = JSON.parse(UrlFetchApp.fetch(url).getContentText())
var data = json.data.owner
var data2 = json.data.rank
var data3 = json.data.total
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('WalletRank')
sh.getRange(2,1,data.length,data[0].length).setValues(data)
sh.getRange(2,2,data2.length,data2[0].length).setValues(data2)
sh.getRange(2,3,data3.length,data3[0].length).setValues(data3)
}
Try
function importRank(){
url = 'https://public-api.solscan.io/token/holders?tokenAddress=sinjBMHhAuvywW3o87uXHswuRXb3c7TfqgAdocedtDj&offset=0&limit=max'
var json = JSON.parse(UrlFetchApp.fetch(url).getContentText())
var data=[]
Logger.log(json.total)
json.data.forEach(function(x){
data.push([x.owner,x.rank,x.amount])
})
var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('WalletRank')
sh.getRange(2,1,data.length,data[0].length).setValues(data)
sh.getRange(2,4).setValue(json.total)
}
Explanation
the structure is as follows
{"data":[
{"address":"__________","amount":________,"decimals":__,"owner":"___","rank":_},
...
],"total":_____}
that means that data is an array [] of multi elements which contains each of them {} address, amount, decimals, owner and rank
Reference
forEach

TypeError when cell contains double figures

The code below is working how I want it too except when cell A1 is large then 9. As soon as I enter anything greater like 10 or 11, I get the error
"TypeError: theSplitString.split is not a function"
What am I doing wrong?
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssSheet = ss.getSheetByName('Quote List');
var copyViewQuote = ss.getSheetByName('New Job');
var quoteNuber = copyViewQuote.getRange("A1").getValue();
Logger.log(+quoteNuber)
var theSplitString = ssSheet.getRange("M" + quoteNuber).getValue(); // WHICH PARTS STRING TO USE
var thePartAmount = ssSheet.getRange("N" + quoteNuber).getValue(); // GIVES YOU THE NUMBER OF PARTS IN THE STRING
var spiltstring = theSplitString.split(","); // WHAT SPILTS THE PARTS IN THE STRING
var data = 1; // START FROM 1 AS 0 GIVES UNDEFINED AS FIRST PART OF STRING
var partListNumer = 1;
copyViewQuote.getRange('H5:L200').clearContent();
for(row=5; row<=5+thePartAmount; row++){
//if (partListNumer <= thePartAmount){
//copyViewQuote.getRange(row, 8).setValue(partListNumer);
//partListNumer = partListNumer + 1
//}
//each spiltstring array data is put on Column 6 or column F that starts from row 10 to 10 + length of the splitstring array
copyViewQuote.getRange(row,9).setValue(spiltstring[data]);
data++;
copyViewQuote.getRange(row,10).setValue(spiltstring[data]);
data++;
copyViewQuote.getRange(row,11).setValue(spiltstring[data]);
data++;
copyViewQuote.getRange(row,12).setValue(spiltstring[data]);
data++;
}
}
Looks like you're trying to split the value of the cell, which can only be done on strings.
Try this on your sheet:
This will force all call data to be strings, which you can work a split function on. That should fix the issue.

BarCode Maker (pulling data from other sheets)

I am working on a function that will construct a bar code from a few data points within my spreadsheet for items in an inventory.
My issue/question is two fold:
When running through the for loop, I am getting this error message "TypeError: Cannot read property "length" from null."
While it looks like I have successfully loaded the ranges I want into veritable, I am not entirely sure as to how to call them, could you please tell me if I am calling them correctly within the if statement?
function barCodeBuilder(stockName) {
// Setting Ranges from prices table
var sheet = SpreadsheetApp.getActive();
var listNameRange = sheet.getRangeByName('MeterailsPricing!ITEM NAME');
var listSkuRange = sheet.getRangeByName('MeterailsPricing!INVENTORY SKU');
var listPriceRange = sheet.getRangeByName('MeterailsPricing!PRICE');
var listSupRange = sheet.getRangeByName('MeterailsPricing!SUPPLIER CODE');
//Errors begin here = TypeError: Cannot read property "length" from null
for(listCounter = 0; listCounter < listNameRange.length; listCounter++){
if(stockName == listNameRange[listCounter][0]){
var barCode = {
sku:listSkuRange[listCounter][0],
supCode:listSupRange[listCounter][0],
price:listPriceRange[listCounter][0]};
}
}
//final bar code assembly
var finalBarCode = barCode.sku + "-" + barCode.supCode + "-" + barCode.price;
return finalBarCode;
}
And as always, thank you all for taking the time to help me out.
Talk to you soon.
Greg R.
Error "TypeError: Cannot read property "length" from null." occurred if there were no matches for the regular expression. Because of this, listNameRange will be null. You need to check for this before your loop.
Sample code snippet from this SO answer:
if (templateVars !== null) {
for (var i = 0; i < templateVars.length; i++) {
...
}
}
Here are some related SO posts which might help:
TypeError: Cannot read property "length" from null
Google Spreadsheets script to delete any rows where a string is found

Type Error: Cannot read property "length" from undefined in Google Apps Script

I'm trying to use #serge 's script to pull emails into a spreadsheet.
I'm getting a type error: "Cannot read property "length" from undefined".
Any help figuring out the problem would be much appreciated.
function getMessagesWithLabel() {
var destArray = new Array();
var threads = GmailApp.getUserLabelByName('CDC Health Alerts').getThreads(1,30);
for(var n in threads){
var msg = threads[n].getMessages();
var destArrayRow = new Array();
destArrayRow.push('thread has '+threads[n].getMessageCount()+' messages');
for(var m in msg){
destArrayRow.push(msg[m].getSubject());
}
destArray.push(destArrayRow);
}
Logger.log(destArray);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
if(ss.getLastRow()==0){
sh.getRange(1,1).setValue('getMessagesWithLabel() RESULTS')
};
sh.getRange(ss.getLastRow()+1,1,destArray.length,destArray[0].length).setValues(destArray)
}
The only line that the length() property is used on, is the very last line.
sh.getRange(ss.getLastRow()+1,1,destArray.length,destArray[0].length).setValues(destArray)
So the array named destArray is not getting any data pushed into it. If you check the value that destArray has in it, it will show a value of undefined. You will need to start at the top of the code, and check critical points for what is getting assigned to variables. For example:
var threads = GmailApp.getUserLabelByName('CDC Health Alerts').getThreads(1,30);
Logger.log('threads: ' + threads);
Note the Logger.log() statement in the above code. Add a Logger.log('Some Text: ' + variableName); statement, then VIEW the LOGS, and see if the variable 'threads` is getting what is needed assigned to it.
Keep doing this until you find a problem. Even though the error is coming from the last line, there is an error higher up in the code, because the variable destArray is not getting assigned values.

nested listbox google apps script

Im trying to workaround the impossibility of creating nested listboxes in GAS. I created some arrays to populate the listboxes and used the for looping to connect them to their respective listboxes.
The Arrays
var TicketTypeArray=["TICKETTYPE1","TICKETTYPE2","TICKETTYPE3","TICKETTYPE4","TICKETTYPE5","TICKETTYPE6","TICKETTYPE7","TICKETTYPE8","TICKETTYPE9","TICKETTYPE10","TICKETTYPE11","TICKETTYPE12","TICKETTYPE13","TICKETTYPE14"];
var DemandedByArray=["DEMANDEDBY1","DEMANDEDBY1"];
var AnalystArray=["ANALYST1","ANALYST2","ANALYST3","ANALYST4","ANALYST5","ANALYST6","ANALYST7","ANALYST8","ANALYST9"];
var StatusType1Array=["STATUS1","STATUS2","STATUS3"];
var StatusType2Array=["STATUS1","STATUS2","STATUS3"];
var StatusType3Array=["STATUS1","STATUS2","STATUS3"];
Im trying to use a if else loop to nest the next listbox to another one:
if (TicketTypeListBox="TICKETTYPE1")
{
for(var i=0; i<StatusType1Array.length; i++)
{
StatusListBox.addItem(appRegistro.createLabel(StatusType1Array[i])).setItemText(i, StatusType1Array[i]);
}
}
else if (TicketTypeListBox="TICKETTYPE2")
{
for(var i=0; i<StatusType2Array.length; i++)
{
StatusListBox.addItem(appRegistro.createLabel(StatusType2Array[i])).setItemText(i, StatusType2Array[i]);
}
}
else
{
StatusListBox.addItem("Teste");
}
The TicketTypeListBox is:
var TicketTypeListBox = appRegistro.createListBox().setId('TicketType').setName('TicketType');
for(var i=0; i<TicketTypeArray.length; i++)
{
TicketTypeListBox.addItem(appRegistro.createLabel(TicketTypeArray[i])).setItemText(i, TicketTypeArray[i]);
}
To show the panel, im using the code:
panel.add(DataLabel);
panel.add(DataTextBox);
panel.add(TicketIDLabel);
panel.add(TicketIDTextBox);
panel.add(TicketTypeLabel);
panel.add(TicketTypeListBox);
panel.add(DemandedByLabel);
panel.add(DemandedByListBox);
panel.add(AnalystLabel);
panel.add(AnalystListBox);
panel.add(StatusLabel);
panel.add(StatusListBox);
appRegistro.add(panel);
return appRegistro
Now, when i run the script in a Google Sites, i get the error message "Cannot find method add(string)". When debuging, it locates the error just in the line of the TicketTypeListBox.
panel.add(TicketTypeListBox);
What can i do?
This error is coming because in the if block, instead of equality comparator, you have used assignment operator which assigns a string to the list box.
Istead of
if (TicketTypeListBox = "ACCESS CONTROL")
use
if (TicketTypeListBox == "ACCESS CONTROL")
Similarly modify other places also.
I am pretty much sure that still it will not work because, a listbox can not be compared with a string. To create nested listboxes, you will have to use change handler on the lisboxes. Here is a quick example for the same.
var lbArray1 = ['item1', 'item2', 'item3'];
var item1Array = ['item11', 'item12', 'item13'];
var item2Array = ['item21', 'item22', 'item23'];
var item3Array = ['item31', 'item32', 'item33', 'item34'];
function doGet(e){
var app = UiApp.createApplication();
var vPanel = app.createVerticalPanel();
app.add(vPanel);
var lb1 = app.createListBox().setName('lb1').setId('lb1');
for(var i in lbArray1){lb1.addItem(lbArray1[i])};
var lb2 = app.createListBox().setName('lb2').setId('lb2');
//load the 2nd lb with items dependent on first one
for(var i in item1Array){lb2.addItem(item1Array[i])};
vPanel.add(lb1).add(lb2);
//use change handler to dynamically change the 2nd listbox based on the selection of first listbox
var changeHandler1 = app.createServerHandler('updateListBox_').addCallbackElement(vPanel);
lb1.addChangeHandler(changeHandler1);
return app;
}
function updateListBox_(e){
var app = UiApp.getActiveApplication();
//first listbox item selected
var lb1Item = e.parameter.lb1;
var lb2 = app.getElementById('lb2');
lb2.clear();//remove all items from lb2
if(lb1Item == 'item1'){
for(var i in item1Array){lb2.addItem(item1Array[i])};
}
else if(lb1Item == 'item2'){
for(var i in item2Array){lb2.addItem(item2Array[i])};
}
else if(lb1Item == 'item3'){
for(var i in item3Array){lb2.addItem(item3Array[i])};
}
return app;
}