PHPExcel Protect a single column - cell

I have issues with cell protection.
I would like to protect just one column, B for example.
So I tried:
$sheet->getProtection()->setSheet(true);
$highestRow = $sheet->getHighestRow();
$sheet->getStyle('A1:J2000)->getProtection()->setLocked( PHPExcel_Style_Protection::PROTECTION_UNPROTECTED );
for($i=1;$i<=$highestRow;$i++)
{
$sheet->getStyleByColumnAndRow(1,$i)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_PROTECTED);
}
But it's really slow, and not good because if I need to open my sheet again
$highestRow = $sheet->getHighestRow(); will return "J".
Another solution would be to get the last non-empty column, do you know how to do that? Because getHighestRow(Column) return the columns unprotected or empty.

The loop is slow because you're applying the style to each individual cell, rather than to the range of cells demonstrated in your
$sheet->getStyle('A1:J2000)->getProtection()->setLocked( PHPExcel_Style_Protection::PROTECTION_UNPROTECTED );
line
: one call to set the style for a range of 1000 cells is more that 1000 times faster than applying it to each of 1000 cells individually.
$sheet->getHighestDataRow();
will return the highest row in the worksheet that contains actual data values
$sheet->getHighestDataColumn();
is the column equivalent

First you can protect complete sheet. After that you can uprotect others. This code will protect the first column and first
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getStyle('B2:Z400')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);

Related

Google Sheets: How can I extract partial text from a string based on a column of different options?

Goal: I have a bunch of keywords I'd like to categorise automatically based on topic parameters I set. Categories that match must be in the same column so the keyword data can be filtered.
e.g. If I have "Puppies" as a first topic, it shouldn't appear as a secondary or third topic otherwise the data cannot be filtered as needed.
Example Data: https://docs.google.com/spreadsheets/d/1TWYepApOtWDlwoTP8zkaflD7AoxD_LZ4PxssSpFlrWQ/edit?usp=sharing
Video: https://drive.google.com/file/d/11T5hhyestKRY4GpuwC7RF6tx-xQudNok/view?usp=sharing
Parameters Tab: I will add words in columns D-F that change based on the keyword data set and there will often be hundreds, if not thousands, of options for larger data sets.
Categories Tab: I'd like to have a formula or script that goes down the columns D-F in Parameters and fills in a corresponding value (in Categories! columns D-F respectively) based on partial match with column B or C (makes no difference to me if there's a delimiter like a space or not. Final data sheet should only have one of these columns though).
Things I've Tried:
I've tried a bunch of things. Nested IF formula with regexmatch works but seems clunky.
e.g. this formula in Categories! column D
=IF(REGEXMATCH($B2,LOWER(Parameters!$D$3)),Parameters!$D$3,IF(REGEXMATCH($B2,LOWER(Parameters!$D$4)),Parameters!$D$4,""))
I nested more statements changing out to the next cell in Parameters!D column (as in , manually adding $D$5, $D$6 etc) but this seems inefficient for a list thousands of words long. e.g. third topic will get very long once all dog breed types are added.
Any tips?
Functionality I haven't worked out:
if a string in Categories B or C contains more than one topic in the parameters I set out, is there a way I can have the first 2 to show instead of just the first one?
e.g. Cell A14 in Categories, how can I get a formula/automation to add both "Akita" & "German Shepherd" into the third topic? Concatenation with a CHAR(10) to add to new line is ideal format here. There will be other keywords that won't have both in there in which case these values will just show up individually.
Since this data set has a bunch of mixed breeds and all breeds are added as a third topic, it would be great to differentiate interest in mixes vs pure breeds without confusion.
Any ideas will be greatly appreciated! Also, I'm open to variations in layout and functionality of the spreadsheet in case you have a more creative solution. I just care about efficiently automating a tedious task!!
Try using custom function:
To create custom function:
1.Create or open a spreadsheet in Google Sheets.
2.Select the menu item Tools > Script editor.
3.Delete any code in the script editor and copy and paste the code below into the script editor.
4.At the top, click Save save.
To use custom function:
1.Click the cell where you want to use the function.
2.Type an equals sign (=) followed by the function name and any input value — for example, =DOUBLE(A1) — and press Enter.
3.The cell will momentarily display Loading..., then return the result.
Code:
function matchTopic(p, str) {
var params = p.flat(); //Convert 2d array into 1d
var buildRegex = params.map(i => '(' + i + ')').join('|'); //convert array into series of capturing groups. Example (Dog)|(Puppies)
var regex = new RegExp(buildRegex,"gi");
var results = str.match(regex);
if(results){
// The for loops below will convert the first character of each word to Uppercase
for(var i = 0 ; i < results.length ; i++){
var words = results[i].split(" ");
for (let j = 0; j < words.length; j++) {
words[j] = words[j][0].toUpperCase() + words[j].substr(1);
}
results[i] = words.join(" ");
}
return results.join(","); //return with comma separator
}else{
return ""; //return blank if result is null
}
}
Example Usage:
Parameters:
First Topic:
Second Topic:
Third Topic:
Reference:
Custom Functions
I've added a new sheet ("Erik Help") with separate formulas (highlighted in green currently) for each of your keyword columns. They are each essentially the same except for specific column references, so I'll include only the "First Topic" formula here:
=ArrayFormula({"First Topic";IF(A2:A="",,IFERROR(REGEXEXTRACT(LOWER(B2:B&C2:C),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))) & IFERROR(CHAR(10)&REGEXEXTRACT(REGEXREPLACE(LOWER(B2:B&C2:C),IFERROR(REGEXEXTRACT(LOWER(B2:B&C2:C),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))),""),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))))})
This formula first creates the header (which can be changed within the formula itself as you like).
The opening IF condition leaves any row in the results column blank if the corresponding cell in Column A of that row is also blank.
JOIN is used to form a concatenated string of all keywords separated by the pipe symbol, which REGEXEXTRACT interprets as OR.
IFERROR(REGEXEXTRACT(LOWER(B2:B&C2:C),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))) will attempt to extract any of the keywords from each concatenated string in Columns B and C. If none is found, IFERROR will return null.
Then a second-round attempt is made:
& IFERROR(CHAR(10)&REGEXEXTRACT(REGEXREPLACE(LOWER(B2:B&C2:C),IFERROR(REGEXEXTRACT(LOWER(B2:B&C2:C),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>""))))),""),JOIN("|",LOWER(FILTER(Parameters!D3:D,Parameters!D3:D<>"")))))
Only this time, REGEXREPLACE is used to replace the results of the first round with null, thus eliminating them from being found in round two. This will cause any second listing from the JOIN clause to be found, if one exists. Otherwise, IFERROR again returns null for round two.
CHAR(10) is the new-line character.
I've written each of the three formulas to return up to two results for each keyword column. If that is not your intention for "First Topic" and "Second Topic" (i.e., if you only wanted a maximum of one result for each of those columns), just select and delete the entire round-two portion of the formula shown above from the formula in each of those columns.

How to collect data and headers for non blank cells in a row in Sheets

I cannot find a solution to my problem:
I have a sheet with ~290 rows and ~80 columns. The first row and column are fixed/header.
I would like to collect non-blank values and their header into column B.
I've tried to search for solutions, but I'm not as good at excel, so I cannot wrap my head around most of the advice that I've found.
In Google Sheets you could use an Array formula. I got this:
The formula I've used:
=ArrayFormula(CONCATENATE(IF(--(C2:G2<>"")*COLUMN($C$1:$G$1)<>0;$C$1:$G$1&" "&C2:G2;"")))
This is how it works:
(--(C2:G2<>"") will return an array of 0 and 1 if the cell is blank or not
COLUMN($C$1:$G$1) will return an array of column numbers of each cell
(C2:G2<>"")*COLUMN($C$1:$G$1) we multiply both arrays, so we will get an array of column numbers of non blank cells and 0 of blank cells
<>0;$C$1:$G$1&" "&C2:G2;"") We check if each number in the array obtained in step 3 is 0 or not. If it's 0, it returns a null value, if not, it returns the value of cell
CONCATENATE will concatenate all values from previous array (step 4) so we concatenate null values with real values of non blank cells.
Not sure if this will make the sheet load slower if you have too many records.
Hope this helps
Excel is not the same Google Sheets
=ARRAYFORMULA(TRIM(REGEXREPLACE(
TRANSPOSE(
QUERY(TRANSPOSE(IF(C2:F13<>"",C1:F1 & ", ","")),,99^99)
),
"((\s+)|(,\s*$))",
" "
)))
My sample
use:
=ARRAYFORMULA(REGEXREPLACE(TRIM(TRANSPOSE(QUERY(TRANSPOSE(
IF(C2:G<>"", C1:G1&" "&C2:G&",", )),,99^99))), ",$", ))

How to count the number of blank cells in one column based on the first blank row in another column

I have a spreadsheet set up with tv program titles in column B, the next 20 or so columns are tracking different information about that title. I need to count the number of blank cells in column R relating to the range in column B that contains titles (ie, up to the first blank row in column B.)
I can easily set up a formula to count the number of empty cells in a given range in column R, the problem is as I add more titles to the sheet I would have to keep updating the range in the formula [a simple =COUNTIF(R3:R1108, "")]. I've done a little googling of the problem but haven't quite found anything that fits the situation. I thought I would be able to get the following to work but I didn't fully understand what was going on with them and they weren't giving the expected results.
I've tried these formulas:
=ArrayFormula(sum(MIN("B3:B"&MIN(IF((R3:R)>"",ROW(B3:B)-1)))))
=ArrayFormula(sum(INDIRECT("B3:B"&MIN(IF((R3:R)>"",ROW(B3:B)-1)))))
And
=if(SUM(B3:B)="","",SUM(R3:R))
All of the above formulas give "0" as the result. Based on the COUNTIF formula I have set up it should be 840, which is a number I would expect. Currently, there are 1106 rows containing data and 840 is a reasonable number to expect in this situation.
Is this what you're looking for?
=COUNTBLANK(INDIRECT(CONCATENATE("R",3,":R",(3+COUNTA(B3:B)))))
This counts the number of non-blank rows in the B column (starting at B3), and uses that to determine the rows to perform COUNTBLANK in, in column R (starting at R3). CONCATENATE is a way to give it a range by adding strings together, and the INDIRECT allows for the range reference to be a string.
a proper way would be:
=ARRAYFORMULA(COUNTBLANK(INDIRECT(ADDRESS(3, 18, 4)&":"&
ADDRESS(MAX(IF(B3:B<>"", ROW(B3:B), )), 18, 4)))
or shorter:
=ARRAYFORMULA(COUNTBLANK(INDIRECT("R3:"&
ADDRESS(MAX(IF(B3:B<>"", ROW(B3:B), )), 18, 4))))
or shorter:
=ARRAYFORMULA(COUNTBLANK(INDIRECT("R3:R"&MAX(IF(B3:B<>"", ROW(B3:B), ))))

Formula to return TRUE if Criteria Matches?

The reason I am posting this question is that combining Index and Match functions only searches for first qualifying row from top-down and I am needing to find next row up from current that matches as part of my formula.
The complete formula I am trying to construct is to return TRUE in cell "C4" if equal to the row that has the lowest value in column "A" from just above a value (nonblank) in column "C" to just before numbers in column "B" go above 55. So in this case, it would return TRUE in cell "C4" because for the blue highlighted area value 28.28 is lowest in column "A".
Secondarily not sure if INDIRECT function is best to use since I have a few hundred of these in my sheet. Is this a resource hog when I need these to calculate quickly???
I have it posted here and am posting it here because I am trying to get this to work in Sheets which I know is often different than Excel.
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_win10-mso_2016/formula-to-return-true-if-criteria-matches/02834e93-c29f-449d-ace0-98722c399e63?tm=1568399215380
perhaps like this:
=ARRAYFORMULA(IF(A1:A=MIN(FILTER(A1:A, B1:B<55)), TRUE, ))

Arrayformula count how many cells in a row are less than cells in another row only if both are nonzero/nonblank

I found this formula, =arrayformula(sumproduct($C$24:$C$31<B$24:B$31)) but I need it to ignore if one or both cells are 0 or blank.
I've tried and, and countifs, but I can't seem to figure it out.
=arrayformula(sumproduct($C$24:$C$31<B$24:B$31))
I want a number 0-8 for counting how many cells in the particular row are less than the compared row, but (i.e. 0<25) needs to be ignored.
try it like this:
=ARRAYFORMULA(SUM(IF((C2:C < B2:B) * (C2:C<>0), 1, 0)))