I have a column and I'd like to get the column following that particular column.
nhood,column,variablecolumn
Treasure Island,3,5
McLaren Park,1,2
Tenderloin,28,112
Lakeshore,14,8
Chinatown,15,103
I know the name of the second column, but in my dataset the name of the third column changes.
I tried variants on this idea. I'm trying to get the number "5" from the first row.
parseFloat(data[1]["column"+1]);
parseFloat(data[1]["column"]+1);
Another idea is to create an array with the column names, pull the index for "column" and then use index + 1 when I pull the data in that particular command.
Got it. Like mentioned, you can turn the column names into an array and then pull the index for a particular array. In this case, you can use col as a stand-in for the variablecolumn name.
var valueKey = data.columns;
var col = valueKey[valueKey.indexOf(dataset)+1];
Related
I am a non-programmer and quite new to Office Scripts and I would love some help.
I am currently trying to delete entire rows if the cell in the "Change Flag" column, which happens to be the second column on the Excel sheet, contains the word "Delete" or "Deleted".
I managed to delete rows if they contain the word "Delete" with the following script but could not make the script cells with "Deleted" too. I tried throwing brackets in there but it unfortunately did not work.
Can someone suggest a best practice to handle the deletion of rows based on multiple text matches (delete if x = y OR z)?
function main(workbook: ExcelScript.Workbook) {
// Get the used range on the current sheet.
const currentSheet = workbook.getActiveWorksheet();
let table = workbook.getTables()[0];
// Get the RangeAreas object for each cell with a formula.
const usedRange = currentSheet.getUsedRange();
//get the values of the range
let values = usedRange.getValues();
//get the row count of the range
let rowCount = usedRange.getRowCount();
//starting at the last row, check if the cell in column '1' equals to 'Delete'. If it is, then delete the entire row.
for (let i = rowCount - 1; i >= 0; i--) {
if (values[i][1] == "Delete") {
usedRange.getCell(i, 1).getEntireRow().delete(ExcelScript.DeleteShiftDirection.up)
}
}
}
Your if statement should look like the below:
if (values[i][1] == "Delete" || values[i][1] == "Deleted")
If you are looking for more general tutorials, here is a page I found that explains if statements a little more: https://www.w3schools.com/jsref/jsref_if.asp
You can use the includes() method of the string object to do this. To use that method, you'd have to cast the value to a string using the toString() method like so:
if (values[i][1].toString().includes("Delete"))
Using includes, it will flag for both Delete and Deleted.
The words Delete and Deleted may be spelled in a variety of different ways (e.g. delete and deleted, DELETE and DELETED, etc.). Those different spellings will not be flagged by includes(). If want those different spellings to be evaluated in the same way, you can use a method like toLowercase() to do that. After calling that method, you'd just provide the includes() method with a lowercase version of the spelling like so:
if (values[i][1].toString().toLowerCase().includes("delete"))
This approach will also flag for any other text that includes delete. So if you have text in a cell like "this row should not be deleted" this code will flag that text as well.
So if I have a Javascript array of strings, how do I append each element to a row in the Postgres text[] column? This is what I'm currently using, but it's not yielding the desired result (which is just making the text array in the column of {"1","2","3"}.
let addNumbers = 'UPDATE numTable SET column1 = array_append(columnName, ($1)) WHERE uid = ($2)'
let x = ["1","2","3"]
I'm querying to the db with
query(addNumbers, [x, uid]);
I've tried using array_append but this results in the column getting a value of
{"{\"1\",\"2\",\"3\"}"}
I'd prefer not to iterate and append each one individually because this'll obviously increase the number of database calls--any help is much appreciated!
I'm trying to find the index of a value in an array. The array is a spreadsheet, and I want to search down the rows till it finds the value in the active cell and returns the row number/index.
I have tried this searching across columns and it works fine, but when trying to search down rows it does something I can't quite figure out.
var datass =SpreadsheetApp.getActiveSpreadsheet().getSheetByName('####')
var actCell = ss.getActiveCell()
var name =datass.getRange(3,2,datass.getLastRow(),1).getValues();
var nameIndex = name[0].indexOf(actCell.getValue)
nameIndex always returns -1 unless name[#]is the right index number. even if I omit a # it returns -1.
logger.log(name) shows that the pulled is corrected but because of the way it pulls the data index only searches on []
it shows as:
[[##], [##], [##]]
when this functions correctly searching along columns instead of rows, the log shows as
[##,##,##,##]
how do can I search down my list and get the row number based off the value in my active cell?
(Option 1)
As what was mentioned by #Cooper, you can use Range.createTextFinder(findText) to search for a specific string within your selected range.
(Option 2)
You can convert your 2-d array to 1-d array to be able to search for a specific value using Array.prototype.indexOf()
Sample Code:
var datass =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet15");
var actCell = datass.getActiveCell();
var rowStart = 3;
var name =datass.getRange(rowStart,2,datass.getLastRow()-2,1).getValues().flat();
Logger.log(name);
Logger.log(actCell.getA1Notation());
Logger.log(actCell.getValue());
var nameIndex = name.indexOf(actCell.getValue());
Logger.log("match found at row: "+(rowStart+nameIndex));
What it does?
Select a sheet and get its active cell.
Get the values of a given range. This will return a 2-d array values. Convert it to 1-d array using Array.prototype.flat(). Your array will be from this [[##], [##], [##]] to [##,##,##]
Use Array.prototype.indexOf() to search for a value within the array.
Note:
When you select the range of values, you start at row3. To get the actual row index of the matched value you also need to add an offset 3 to the index found in your name array
Output:
4:59:23 AM Notice Execution started
4:59:24 AM Info [A, B, C, D, E, F, G]
4:59:24 AM Info A1
4:59:24 AM Info D
4:59:24 AM Info match found at row: 6
4:59:24 AM Notice Execution completed
I have 10 rows of data on my input step, i transform them in a for-loop and i should get more than 10 rows, but in this case i get the last transform of each iteration that the loop have for each data
I tried to use appendToFile() but the result data is not useful and pentaho read it as a unique header
On my alert() method i can see that the for loop transform the data.
var PERIODO = 2
var i
var fecha_final
//var ruta_acess ="D:\TEST.accdb"
//var contenido
////var contenido2
//var arreglo_completo
for (i=0; i<=PERIODO; i++){
fecha_final = dateAdd(FECHA_INICIO,"d",i)
Alert(i)
}
As I show in the below photo i get only 10 records and in the other photo appears the result that i want that are the results data of each iteration of the for-loop
Modified JavaScript value photo:
Expected result:
Obtained result:
For loops are not really a thing in PDI. Transformations work on sets of rows that flow through the steps, so it's best for performance and stability to use that mindset.
In your scenario each incoming row should end up as three copies, but with different calculated values based on a single new field (with values 0,1,2).
The way to do this in PDI is with a Join rows (cartesian product) step. It takes two sets of input rows and outputs a row for every combination of input rows, possibly filtered by defining a key field that has to match. So if you have 10 rows in the main input and 3 rows in the second, it will output 30 rows.
You will first need to create a data grid as the second input. Define a single integer field, name it something clear and on the second tab fill three rows with 0, 1 and 2 respectively.
Connect both inputs to the Join rows step. You don't need to configure any matching key.
The output of the Join step will be three rows for each input row, one with each of the values 0, 1, 2. Connect that output to a Calculator step and use the calculation Date A + B days to replace the logic from your javascript step.
what i mean is that in the obtained result photo the "i" variable only shows the value of "3" and i would like to have "1", "2" and "3"
to solve this i used
var row = createRowCopy(getOutputRowMeta().size())
var idx = getInputRowMeta().size()
row[idx++] = DPROCESS
this add a row for each result of the iteration.
before the tranformation result showed to me only the last value of each loop.
I want to filter data on the basis of number of rows and columns provided by user.
I am getting the data on controller after reading this excel file:
this is the column header values which I am getting after user has passed 5 as input columns:
This is the row data which I am getting after user has given 4 rows as input to be rendered, here there is data of 4 rows:
Inside each row, there is key value pair containing column name and the cell value:
I want to filter this row data on the basis of the header names which are coming through column so that the row has only the values till the column is defined. Currently I am getting entire row values.
I have written a logic like this but this is not working.
var rowData=[];
for(var i=0;i<headerNames.length;i++){//headerNames contains column names
for(var j=0;j<data.length;j++){//data contains the rows data
for(var keyName in data[j])
if(angular.equals(keyName,headerNames[i])){
rowData.push({'keyName':data[j][keyName]})//I want only the key,values which matches the column names. I want to set the keyName value as array key but I am not getting its value instead it is coming like keyName:18
}
}
}
}
Adding the plunker for the code.
http://plnkr.co/edit/28Z44xDBug7nFCQnKZJL?p=preview
I am able to filter the data on UI and get only the row and column data as per user input.
But i need the same data on controller, so that i can save it in MongoDb. I want to filter the row data as per the column input. So, that i only get the row data till the column is defined.
Please suggest how can i filter the data and can splice the row values so that in row i can have value upto the column numbers defined. For eg, if I user have entered 4 rows and 5 columns then in my row Data i can have only values upto 5th column and all other values i can remove from array. in my code currently i am not able to get the key value to be set as array key.
Please help me to resolve this problem.
Looks like you're most of the way there. You just need to assign rowData to be $scope.rowData so that angular can get to it, and have the Angular document show {{rowdata}} (shows $scope.rowData).
You'll probably need to arrange it into columns though so you might do a ng-repeat on rowData, with variable=row, and in TR TD tags show row.1
eg something like ..
<tr ng-repeat="row in rowData">
<td>{{row.1}}</td>
<td>{{row.2}}</td>
</tr>