How can I extract data from a specific column in a table, then assign it to array of variables using loop? - katalon-studio

I want to extract all the data from column B, then assign it to variables using for loop. I'm using this code, but it is looping wrong.
Ex:
Total row count = 10
Variable 1 to variable 10 are having the same data; then it will loop 10x. So the final values of array variables are all the same :(
for (int getAllAccts = 1; getAllAccts <= TotalRowCount.size(); getAllAccts++) {
try{
String[] accName = new String[TotalRowCount.size()]
for(int accNameCount=1; accNameCount < TotalRowCount.size(); accNameCount++){
accName[accNameCount] = driver.findElement(By.xpath("//*[#id='tbl-table1']/tbody/tr[" + (getAllAccts)+ "]/td[2]/a")).getText();
log.logWarning('Customer Name' + accNameCount + ' ' + accName[accNameCount])
}
}
catch(org.openqa.selenium.StaleElementReferenceException ex) {
String[] accName
for(int accNameCount=1; accNameCount < TotalRowCount.size(); accNameCount++){
accName[accNameCount] = driver.findElement(By.xpath("//*[#id='tbl-table1']/tbody/tr[" + (getAllAccts)+ "]/td[2]/a")).getText();
log.logWarning('Customer Name' + accNameCount + ' ' + accName[accNameCount])
}
}
}

The getAllAccts is always the same inside of the inner (nested) for loop. So all the accNames will have the same value.
Change
accName[accNameCount] = driver.findElement(By.xpath("//*[#id='tbl-table1']/tbody/tr[" + (getAllAccts)+ "]/td[2]/a")).getText();
to
accName[accNameCount] = driver.findElement(By.xpath("//*[#id='tbl-table1']/tbody/tr[" + (accNameCount)+ "]/td[2]/a")).getText();

Related

Is there a better way to calculate the moving sum of a list in flutter

Is there a better way to calculate a moving sum of a list?
List<double?> rollingSum({int window = 3, List data = const []}) {
List<double?> sum = [];
int i = 0;
int maxLength = data.length - window + 1;
while (i < maxLength) {
List tmpData = data.getRange(i, i + window).toList();
double tmpSum = tmpData.reduce((a, b) => a + b);
sum.add(tmpSum);
i++;
}
// filling the first n values with null
i = 0;
while (i < window - 1) {
sum.insert(0, null);
i++;
}
return sum;
}
Well, the code is already clean for what you need. Maybe just some improvements like:
Use a for loop
You can use the method sublist which creates a "view" of a list, which is more efficient
To insert some values in the left/right of a list, there is a specific Dart method called padLeft, where you specify the lenght of the list which you want it to become (first parameter), then the value you want to use to fill it (second parameter). For example, if you have an array of N elements, and you want to fill it with X "null"s to the left, use padLeft(N+X, null).
List<double?> rollingSum({int window = 3, List data = const []}) {
List<double?> sum = [];
for (int i = 0; i < data.length - window + 1; i++) {
List tmpData = data.sublist(i, i + window);
double tmpSum = tmpData.reduce((a, b) => a + b);
sum.add(tmpSum);
}
sum.padLeft(window - 1, null);
return sum;
}
if I understand your problem correctly you can just calculate the window one time and in one loop you can for each iteration you can add the current element to the sum and subtract i - (window - 1)
so for an input like this
data = [1,2,3,4,5,6]
window = 3
the below code will result in [6,9,12,15]
int sum = 0;
List<double> res = [];
for (int i = 0;i<data.length;i++) {
sum += data[i];
if (i < window - 1) {
continue;
}
res.add(sum);
sum -= data[i - (window - 1)]; // remove element that got out of the window size
}
this way you won't have to use getRange nor sublist nor reduce as all of those are expensive functions in terms of time and space complexity

Issues with naming ranges for charts within the Google Spreadsheet Script

I've been trying for days to create charts with an intelligent range, that differs when the data in the google spreadsheet is updated. However i succeeded doing so, i can't get the .setOption aspect to work. I want for example, a title, description etc with the chart. But this is not the main issue since i can insert there by hand.
More important however is the range name, because there isn't when i use the script. So, within the chart it is not possible to see what each column represents, and i really want to fix that. I tried to use the .setNamedRange() aspects, but that is not working.
Someone who can help me with that?
function check() {
var sheet = SpreadsheetApp.getActiveSheet();
var end = sheet.getLastRow();
var start = (end - 5);
var endnew = (end - 4);
var startnew = (end - 6);
if(sheet.getCharts().length == 0){
Logger.log("Er is geen grafiek");
var chartBuilder = sheet.newChart()
.asColumnChart().setStacked()
.addRange(sheet.getRange("A" + startnew + ":" + "A" + endnew)) // should have a name
.addRange(sheet.getRange("B" + startnew + ":" + "B" + endnew)) // should have a name
.addRange(sheet.getRange("E" + startnew + ":" + "E" + endnew)) //should have a name
.setOption('title', 'Effectief gebruik kantoorruimte') //not working
.setPosition(10, 10, 0, 0)
var chart = chartBuilder.build();
sheet.insertChart(chart);
}
else{
Logger.log("Er is wel een grafiek");
var charts = sheet.getCharts();
for (var i in charts) {
var chart = charts[i];
var ranges = chart.getRanges();
var builder = chart.modify();
for (var j in ranges) {
var range = ranges[j];
builder.removeRange(range);
builder
.addRange(sheet.getRange("A" + (start) + ":" + "A" + end)) //should have a name
.addRange(sheet.getRange("B" + (start) + ":" + "B" + end)) //should have a name
.addRange(sheet.getRange("E" + (start) + ":" + "E" + end)) // should have a name
.setOption('title', 'Effectief gebruik kantoorruimte')
.build();
sheet.updateChart(builder.build());
}
}
}
}
I'm assuming that this code is the issue?
builder
.addRange(sheet.getRange("A" + (start) + ":" + "A" + end))
Maybe try using the JavaScript toString() method to make sure that your text formula is working.
.addRange(sheet.getRange("A" + start.toString() + ":" + "A" + end.toString()))
There is a different format that you can use:
getRange(row, column, numRows, numColumns)
So, it would be:
getRange(start, 1, 1, numColumns)
That starts on row "start" in column A. It gets one row of data, and how ever many number of columns.

Printing the values from the corresponding array

Suppose:
1 A
2 B
3 C
I need to print the value corresponding to 1-> A. I have put each in an array:
d1[1,2,3] and s1[A,B,C]. Now I need to print the value in the form shown in the above:
d1[0] s1[0]
1 A
How can I do this using UnityScript? In the program, I did id printing in this format:
1 A
1 B
1 C
2 A
2 B
2 C
What you have probably done, and I'm guessing without seeing the code is something along that you have a for loop within a for loop which means you're processing the first item in the first array and then all items in the second:
for (var i = 0; i < d1.Length; i++) {
for(var j = 0; j < s1.length; j++ {
Debug.log(d1[i] + " " + s1[j])
}
}
Your options depend on the array sizes and how you want to handle them. For example if you know they're the same size consistently then
for(var i = 0; i < d1.Length; i++) {
Debug.log(d1[i] + " " + s1[i])
}
should work. I would wonder if what you're trying to achieve could be done with a different data structure such as a dictionary, etc.

Why is the following for loop not printing anything?

ages = {paul: 11, rick: 7 }
for n,a in ages
console.log("names: #{n}, ages: #{a}")
So nothing is being printed in the console. What I'm doing wrong?
That translates into the following JavaScript:
var a, ages, n, _i, _len;
ages = {
paul: 11,
rick: 7
};
for (a = _i = 0, _len = ages.length; _i < _len; a = ++_i) {
n = ages[a];
console.log("names: " + n + ", ages: " + a);
}
Note that it's trying to loop through the ages object using the length property, which doesn't exist.
Change your script so the loop uses for n,a of ages syntax instead (relevant documentation), and your code translates into valid JavaScript and prints to the console.
var a, ages, n;
ages = {
paul: 11,
rick: 7
};
for (n in ages) {
a = ages[n];
console.log("names: " + n + ", ages: " + a);
}

highchart - average value of serie for shown period

Using Highstock, I've a serie timestamp / value
with different rangeselectors (hour, day, week, month,...) or zoomX
I want to display the average value for the displayed time period.
Now, I can compute the average of the overall series data:
for (i = 0; i < chart.series[0].yData.length; i++) {
total += chart.series[0].yData[i];
}
seriesAvg = (total / chart.series[0].yData.length).toFixed(4); // fix decimal to 4 places
$('#report1').html('<b>Average:</b>: '+ seriesAvg);
How to compute the average only on the displayed datapoints ? And to refresh automatically after zoom ?
Thank you
Use series.processedYData or series.points.
I implemented the same but I notice that processedYdata is loading also one data more than the one showed at the screen.
for that reason i added one additional command to remove the first processedYData:
processedYData.splice(0, 1);
below is the final result of the function
function showStat(objHighStockchart) {
for (j = 0; j < (json_data.length); j++) {
var seriesAvg = 0,
processedYData = objHighStockchart.series[j].processedYData;
processedYData.splice(0, 1);
var seriesMin = Math.min.apply(null, processedYData);
var seriesMax = Math.max.apply(null, processedYData);
var i = 0
var total = 0;
console.log(processedYData);
for (i = 1; i < processedYData.length; i++) {
total += processedYData[i];
}
seriesAvg = (total / processedYData.length).toFixed(2); // fix decimal to 4 places
$('#container_stat' + j).html(
'<br>Statistics for ' + objHighStockchart.series[j].name + '<br>' +
'Total: ' + total + ' logs<br>' +
'Min: ' + seriesMin + ' logs<br>' +
'Avg: ' + seriesAvg + ' logs<br>' +
'Max: ' + seriesMax + ' logs<br>'
+ '---' + processedYData
);
}
};