why are there no objects added to my table - arrays

I'm working on a lua script where I need a list of buttons to display. I have the following method to add a button to the list buttons:
function addButton(name, text, func, activeColor, passiveColor, xmin, xmax, ymin, ymax, type)
buttons[name] = {}
buttons[name]["text"] = text
buttons[name]["func"] = func
buttons[name]["active"] = false
buttons[name]["activeColor"] = getColorOrDefault(activeColor, ACTIVECOLORDEFAULT)
buttons[name]["passiveColor"] = getColorOrDefault(passiveColor, PASSIVECOLORDEFAULT)
buttons[name]["xmin"] = xmin
buttons[name]["xmax"] = xmax
buttons[name]["ymin"] = ymin
buttons[name]["ymax"] = ymax
buttons[name]["type"] = type
print("added: "..table.getn(buttons))
end
This function is called twice to add 2 buttons, but the output is:
added: 0
added: 0
what could be a reason my elements are not added to the table?

They're being added--You're just using the wrong method to detect them.
As you can read about in this question, using table.getn only returns the amount of array slots (numeric indexes, such as buttons[0] or buttons[23]. Note that this only counts as an array slot if there are no null indexes in between buttons[1] and buttons[23]!
Using this debugging code, we'll see that your items are indeed being added to the buttons table:
for name, button in pairs(buttons) do
print(name, button)
end
This is also how you'd get a list of your buttons :-)

Related

How to access an array of UDT <type>

In the Pine script reference manual the is a code snippet on
https://www.tradingview.com/pine-script-docs/en/v5/language/Objects.html
I want to know how to access the values of the array pivotHighArray.
This is because I don't want to draw a line only between the last pivot high and the previous pivot high. I want to draw a line/lines from the current pivot high to ANY previous pivot highs that meet certain conditions (e.g. previous pivot high must be higher than the current).
//#version=5
indicator("Pivot Points High", overlay = true)
int legsInput = input(10)
// Define the `pivotPoint` UDT containing the time and price of pivots.
type pivotPoint
int openTime
float level
// Create an empty `pivotPoint` array.
var pivotHighArray = array.new<pivotPoint>()
// Detect new pivots (`na` is returned when no pivot is found).
pivotHighPrice = ta.pivothigh(legsInput, legsInput)
// Add a new `pivotPoint` object to the end of the array for each detected pivot.
if not na(pivotHighPrice)
// A new pivot is found; create a new object of `pivotPoint` type, setting its `openTime` and `level` fields.
newPivot = pivotPoint.new(time[legsInput], pivotHighPrice)
// Add the new pivot object to the array.
array.push(pivotHighArray, newPivot)
// On the last historical bar, draw pivot labels and connecting lines.
if barstate.islastconfirmedhistory
var pivotPoint previousPoint = na
for eachPivot in pivotHighArray
// Display a label at the pivot point.
label.new(eachPivot.openTime, eachPivot.level, str.tostring(eachPivot.level, format.mintick), xloc.bar_time, textcolor = color.white)
// Create a line between pivots.
if not na(previousPoint)
// Only create a line starting at the loop's second iteration because lines connect two pivots.
line.new(previousPoint.openTime, previousPoint.level, eachPivot.openTime, eachPivot.level, xloc = xloc.bar_time)
// Save the pivot for use in the next iteration.
previousPoint := eachPivot
I have tried pivotHighArray.level[someindex], array.get(pivotHighArray.level, someindex) but failed.
How do I reference these values?
This question is related to my old question which I'm now trying another approach on since it fails when a new bar is printed both live and during backtesting
You're on the right track. Try:
pivotPoint t = na
if 0 < array.size(pivotHighArray)
t := array.get(pivotHighArray, 0)
plot(na(t) ? na : t.level, color = color.red)
First you just read the array element, after that you can access its properties.

Google Apps Script - Randomizing Dropdown - Selecting Index

What I have seems simple, but looking through SO and other sites hasn't let me figure it out.
H3 and S3 are cells with a dropdown data validation list.
I have a button to randomize my these. This is easy when it's a number, but these are strings.
S3 is the cell for size so I have this randomized.
var randSize = Math.floor(Math.random() * (20 - 1 + 1)) + 1
ss.getRange("S3").setValue(randSize)
I don't understand how to either set a random int to select the value based on array index. Or get a random string value based on the entries in cell H3, then setting H3 to equal the new string.
Name | #
random item | random #
I need a random item to be set randomly from a value from H3.
Issue and solution:
If I understand you correctly, you have a set of string values in a column, and you want to copy a random value from that column to the cell H3.
If that's correct, you can just do the following:
Retrieve the column values to a simple array via getValues() and flat.
Retrieve a random value from that array, using length and Math.
Set that value to your target cell, using setValue.
Code sample:
function getRandomValue() {
const ss = SpreadsheetApp.getActiveSpreadsheet(); // If standalone, use openById/openByUrl
const sourceSheet = ss.getSheetByName("COLUMN_VALUES_SHEET_NAME");
const targetSheet = ss.getSheetByName("H3_SHEET_NAME");
const COLUMN_INDEX = 3; // Change according to your preferences
const FIRST_ROW = 2; // Change according to your preferences
const NUM_ROWS = sourceSheet.getLastRow() - FIRST_ROW + 1;
const array = sourceSheet.getRange(FIRST_ROW, COLUMN_INDEX, NUM_ROWS).getValues().flat();
const randomIndex = Math.floor(Math.random() * array.length);
const randomValue = array[randomIndex];
targetSheet.getRange("H3").setValue(randomValue);
}

Swift - Update and store position of a multiple programmatically created buttons

I have a button which creates other buttons based on the class Clip seen below. Those newly created buttons are added to an array and stored in a plist.
class Clip: Encodable, Decodable {
var name: String = ""
var xCoordinate: Int = 100
var yCoordinate: Int = 300
// more parameter will be added later on e.g color, scale etc..
}
Each button can be moved around the view and the new x & y coordinates are stored in a plist.
#objc func handlePan(sender: UIPanGestureRecognizer){
let uIViewSelected = sender.view!
switch sender.state {
case .began, .changed :
moveViewWithPan(view: uIViewSelected, sender: sender)
break
case .ended:
//Finds the position when the button is no longer being dragged
let x = Int(uIViewSelected.center.x)
let y = Int(uIViewSelected.center.y)
//clipArray[0] need to be the corresponding clicked button e.g clipArray[2]
clipArray[0].xCoordinate = x
clipArray[0].yCoordinate = y
saveData()
break
default:
break
}
}
The above works only if I create one button. When more buttons are added, the above lines only change the first clip from the array. I need a way to update the value to the correct button clicked.
How can identify the array position of the click button as I am creating all them programmatically? At the moment I am placing at value 0 of the clipArray.
clipArray[0].xCoordinate = x
clipArray[0].yCoordinate = y
I am not even sure if using a plist is the best way to store the buttons in the first place.
Any help or documentation would be much appreciated.
Thanks
Following from dfd response, I added tags to each button which are created and it solved the issue for now.
let x = Int(uIViewSelected.center.x)
let y = Int(uIViewSelected.center.y)
//clipArray.append(clip)
var tagNo = uIViewSelected.tag
clipArray[tagNo].xCoordinate = x
clipArray[tagNo].yCoordinate = y

Data and Series label not showing on WinForm Chart control

I am having a problem getting the default WinForms Chart control to work. I have a single chart with a single area. In this area, I want to display three (3) series whose labels are on a single legend.
Each of the value arrays shown in the code below contains six (6) values.
When I run the application the chart is only showing the background with the title and the name of the FIRST series I defined, the others seem to be ignored. Also, no grid and no data points or lines are displayed. The chart is basically blank.
this.chart.SuspendLayout();
this.chart.ChartAreas.Clear();
this.chart.Series.Clear();
ChartType chartType = ChartType.Column;
// prepare the area
const string AREA_NAME = "ChartAreaBP";
ChartArea bpChartArea = new ChartArea(AREA_NAME);
bpChartArea.AxisX.LabelStyle.Format = "dd/MMM\nyyyy";
bpChartArea.AxisX.MajorGrid.LineColor = System.Drawing.Color.LightGray;
bpChartArea.AxisY.MajorGrid.LineColor = System.Drawing.Color.LightGray;
bpChartArea.BackColor = System.Drawing.Color.LimeGreen;
bpChartArea.BackGradientStyle = GradientStyle.DiagonalRight;
bpChartArea.Position.Auto = true;
bpChartArea.InnerPlotPosition.Auto = true;
this.chart.ChartAreas.Add(bpChartArea);
// prepare the values. X is Date/time all other 3 are BYTE/INT
var xvals = from x in items select x.TimeStamp;
var yvalsSys = from y in items select y.Systolic;
var yvalsDia = from y in items select y.Diastolic;
var yvalsRhy = from y in items select y.Rhythm;
// The first series, other 2 omitted from HERE for simplicity
const string SYS_SERIES = "Systolic";
Series sysBPSeries = new Series(SYS_SERIES, 4);
sysBPSeries.ChartType = chartType;
sysBPSeries.ChartArea = AREA_NAME;
sysBPSeries.XValueType = ChartValueType.Auto;
sysBPSeries.YValueType = ChartValueType.Date;
sysBPSeries.XAxisType = AxisType.Primary;
sysBPSeries.YAxisType = AxisType.Primary;
sysBPSeries.Enabled = true;
this.chart.Series.Add(sysBPSeries);
this.chart.Series[SYS_SERIES].Points.DataBindXY(xvals, yvalsSys);
// here the other two series are defined.
But when I run the application only the legend of the FIRST series is shown even though the other two are defined in the code (I omitted them from this listing) just the same way as the first series.
And as I stated above, no grid nor values are shown. However, the chart shown in design mode does show all three labels on the first and only legend and all three lines.

Store formatting information in an array then apply it to a range

I'm trying to create a script that will automatically format a selection based on the formatting of a table in another sheet. The idea is that a user can define a table style for header, rowOdd and rowEven in the Formats sheet, then easily apply it to a selected table using the script.
I've managed to get it working, but only by applying one type of formatting (background colour).
I based my code for reading the code into an array on this article.
As you will hopefully see from my code below, I am only able to read one formatting property into my array.
What I would like to do is read all formatting properties into the array, then apply them to the range in one go. I'm new to this so sorry if my code is a mess!
function formatTable() {
var activeRange = SpreadsheetApp.getActiveSpreadsheet().getActiveRange(); //range to apply formatting to
var arr = new Array(activeRange.getNumRows());
var tableStyleSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Formats"); //location of source styles
var tableColours = {
header: tableStyleSheet.getRange(1, 1, 1).getBackground(),
rowEven: tableStyleSheet.getRange(2, 1, 1).getBackground(),
rowOdd: tableStyleSheet.getRange(3, 1, 1).getBackground()
}
for (var x = 0; x < activeRange.getNumRows(); x++) {
arr[x] = new Array(activeRange.getNumColumns());
for (var y = 0; y < activeRange.getNumColumns(); y++) {
x == 0 ? arr[x][y] = tableColours.header :
x % 2 < 1 ? arr[x][y] = tableColours.rowOdd : arr[x][y] = tableColours.rowEven;
Logger.log(arr);
}
}
activeRange.setBackgrounds(arr);
}
Thanks!
I might be wrong but based from the list of methods given in Class Range, feature to save or store formatting details currently do not exist yet.
However, you may want to try using the following:
copyFormatToRange(gridId, column, columnEnd, row, rowEnd) or copyFormatToRange(sheet, column, columnEnd, row, rowEnd) wherein it copies the formatting of the range to the given location.
moveTo(target) wherein it cuts and paste (both format and values) from this range to the target range.
Did you know that you can get all of the different formatting elements for a range straight into an array?
E.g.
var backgrounds = sheet.getRange("A1:D50").getBackgrounds();
var fonts = sheet.getRange("A1:D50").getFontFamilies();
var fontcolors = sheet.getRange("A1:D50").getFontColors();
etc.
However, there's no way to get all of the formatting in one call unfortunately, so you have to handle each element separately. Then you can apply all of the formats in one go:
targetRng.setFontColors(fontcolors);
targetRng.setBackgrounds(backgrounds);
and so on.

Resources