Phpspreadsheet Search Between Columns - phpspreadsheet

Here's my code.
$highestColumn++;
for ($row = 1; $row <= $highestRow; $row++) {
for ($col = 'A'; $col != $highestColumn; ++$col) {
$val = $sheet2->getCell($col . $row)->getValue();
if($col < 'D'){
//something
}
}}
In if, i'm getting A,B,C columns and getting other 'AA' 'BB' 'CC' 'CB' ... etc. And this is wrong solution for me. Is there any thing to do for this condition? I just want to see A,B,C columns in if.

I solved the problem like this:
$sheet->rangeToArray('BU3:CA3')[0];

Related

Echo error data using str_replace function

I have a problem to replace a certain format with a symbol inside the number. I use below the coding to replace, but not work for me to replace the symbol, the result won't come out.
$try = "100-1-1 abc/bbb";
$try_1 = "100-1-1-16 John/James";
echo(replaceHyphen($try));
echo(replaceHyphen($try_1));
<script>
function replaceHyphen($str) {
$i = -1;
for($i=0; $i < strlen($str); $i++) {
if($str[$i] == "-") break;
}
for($i++; $i < strlen($str); $i++) {
if($str[$i] == "-") $str[$i] = '/';
if($str[$i] == " ") break;
}
return $str;
}
</script>
I want the expected result like below:
100-1/1 abc/bbb
100-/1/16 John/James
Hope someone can guide me on how to solve it. Thanks.

How to compare two column in a spreadsheet

I have 30 columns and 1000 rows, I would like to compare column1 with another column. IF the value dont match then I would like to colour it red. Below is a small dataset in my spreadsheet:
A B C D E F ...
1 name sName email
2
3
.
n
Because I have a large dataset and I want to storing my columns in a array, the first row is heading. This is what I have done, however when testing I get empty result, can someone correct me what I am doing wrong?
var index = [];
var sheet = SpreadsheetApp.getActiveSheet();
function col(){
var data = sheet.getDataRange().getValues();
for (var i = 1; i <= data.length; i++) {
te = index[i] = data[1];
Logger.log(columnIndex[i])
if (data[3] != data[7]){
// column_id.setFontColor('red'); <--- I can set the background like this
}
}
}
From the code you can see I am scanning whole spreadsheet data[1] get the heading and in if loop (data[3] != data[7]) compare two columns. I do have to work on my colour variable but that can be done once I get the data that I need.
Try to check this tutorial if it can help you with your problem. This tutorial use a Google AppsScript to compare the two columns. If differences are found, the script should point these out. If no differences are found at all, the script should put out the text "[id]". Just customize this code for your own function.
Here is the code used to achieve this kind of comparison
function stringComparison(s1, s2) {
// lets test both variables are the same object type if not throw an error
if (Object.prototype.toString.call(s1) !== Object.prototype.toString.call(s2)){
throw("Both values need to be an array of cells or individual cells")
}
// if we are looking at two arrays of cells make sure the sizes match and only one column wide
if( Object.prototype.toString.call(s1) === '[object Array]' ) {
if (s1.length != s2.length || s1[0].length > 1 || s2[0].length > 1){
throw("Arrays of cells need to be same size and 1 column wide");
}
// since we are working with an array intialise the return
var out = [];
for (r in s1){ // loop over the rows and find differences using diff sub function
out.push([diff(s1[r][0], s2[r][0])]);
}
return out; // return response
} else { // we are working with two cells so return diff
return diff(s1, s2)
}
}
function diff (s1, s2){
var out = "[ ";
var notid = false;
// loop to match each character
for (var n = 0; n < s1.length; n++){
if (s1.charAt(n) == s2.charAt(n)){
out += "–";
} else {
out += s2.charAt(n);
notid = true;
}
out += " ";
}
out += " ]"
return (notid) ? out : "[ id. ]"; // if notid(entical) return output or [id.]
}
For more information, just check the tutorial link above and this SO question on how to compare two Spreadsheets.

how to disable multi-column sort in ui-grid

I have a client who specifically does not like the numbers next in the headers of the columns when doing a sort. This is rooted in UI-Grid's multi-sort, which gives each column a numbered priority. Is there a way to disable the multi-sort in order to remove those numbers? I still want to keep sorting activated, but only on one column at a time.
Thanks.
I've had this problem myself. If you look carefully in the ui0grid.js code you'll see that there is (at this time) no option to diable it. The writers of ui-grid state that they would welcome a request for such a function in this thread
However, you want a fix, not a promise ;-)
You can spot how many sortColumns have been chosen in the sortChanged method.
Try something like this:
$scope.gridOptions.onRegisterApi = function(gridApi) {
$scope.gridApi = gridApi;
// Register a handler that is fired everytime someone changd the sort params.
$scope.gridApi.core.on.sortChanged($scope, function(grid, sortColumns) {
if (sortColumns.length > 1) {
// We have more than one sort. Kill the first one.
// If this works we'll only ever have 0, 1 or 2 sortColumns,
// and only ever 2 for the lifetime of this method.
var column = null;
for (var j = 0; j < grid.columns.length; j++) {
if (grid.columns[j].name === sortColumns[0].field) {
column = grid.columns[j];
break;
}
}
if (column) {
sortColumns[1].sort.priority = 1; // have to do this otherwise the priority keeps going up.
column.unsort();
}
}
});
};
This is against the 3.0.0 release of ui-grid.
HTH
To prevent sorting on multiple columns, I added these two lines in the Grid.prototype.sortColumn function, ui-grid.js file.
self.resetColumnSorting(column);
column.sort.priority = undefined;
works for me..
I wanted to limit multiple sort columns to a maximum of 2. This is how I did it.
$scope.gridOptions.onRegisterApi = function(gridApi) {
$scope.gridApi = gridApi;
$scope.gridApi.core.on.sortChanged($scope, function(grid, sortColumns) {
if (sortColumns.length == 3) {
//limit multi column sort to max 2 columns
for (var j = 0; j < grid.columns.length; j++) {
if (grid.columns[j].name === sortColumns[2].name) {
grid.columns[j].sort = {};
break;
}
}
return;
});
};
Looks like this is supported now in the HTML element:
[suppressMultiSort]="true"
This in the latest version. No need for tough scripts.

Google script, manipulate the number value

I have one script behind one spreedsheet and I'm trying to export some cell values to document, and than email the pdv version of temp document..(trash it latter). I have problem with value I get or in the way I'm getting the value from spreedsheet to doc.. I can't manipulate the decimal point..
// fix the price currency display and alignment in GOOGLE DOCUMENT TABLE!
for (var i = 0; i < price.getNumRows(); i++){
for (var j = 0; j < price.getRow(i).getNumCells(); j++){
var temp = price.getCell(i, j);
temp.getChild(0).asParagraph().setSpacingAfter(0);
if((j == 6 || j == 7) && !temp.getText() == "" ) {
(i > 0) ? temp.replaceText(temp.getText(), temp.getText() + " kn") : void false; // skip the first line with header titles...
temp.getChild(0).asParagraph().setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
}
}
}
after (i > 0) there is temp.getText() value.. that sometimes is like: 55,987654 and I would like that to round to two digits.. but can't :(
Thanks for help!
I have found solution..
it's very simple but did take me some time.. hope that can help someone else with similar situation
parseFloat(temp.getText()).toFixed(2) + " kn")
this did the trick and the output is something like: 55,99 kn!

Why is IndexOutOfBoundsException thrown

Below code is throwing an IndexOutOfBoundsException at line Field f = getField(counter);
Why is it being thrown ? Surely the field exists because I am looping based on fieldcount. Or is the list fields in the manager not gauranteed to be sequential? If this is the case how should I delete fields from a screen that are of type - MyButtonField
Thanks
int fieldCount = getFieldCount() - 1;
if(fieldCount > 1){
for(int counter = 0; counter <= fieldCount ; ++counter){
Field f = getField(counter);
if(f instanceof MyButtonField){
delete(f);
}
}
}
You haven't specified what delete(f) does, but if it removes it from the list of fields, then your "valid count" will effectively decrease.
To rewrite this somewhat and fix the problem:
for (int index = getFieldCount() - 1; index >= 0; index--){
Field f = getField(index);
if (f instanceof MyButtonField) {
delete(f);
}
}
This will go from the end of the fields instead of the start, so it doesn't matter if you remove an entry and everything shuffles up - the items which shuffle up will be the ones you've already looked at.
The best way is to use Iterator for iteration then call the method remove().
Example:
for(Iterator it = getFields().iterator();it.hasNext()){
Field f = (Field) it.next();
if(f instanceof MyButtonField){
it.remove();
}
}
The method getFields() has to return a collection of Field elements.

Resources