google sheets Get the last non-empty cell in a ROW - regex

this is the simplified version of my data.
How can I calculate E and F columns? the address format of E column is not important.

put this custom formula to E2 =last_item_index(A2:D)
and here is the custom function code:
/**
* #customfunction
*/
function last_item_index(range) {
const colCount = range[0].length
const lastItemIndices = range.map(row=>{
const indexFirstNonEmpty = row.reverse().findIndex(cell=>cell)
const indexLastNonEmpty = colCount - 1 - indexFirstNonEmpty
return indexFirstNonEmpty>=0? [indexLastNonEmpty, row[indexFirstNonEmpty]] : ['','']
})
return lastItemIndices
}
The value returned in column E will be the zero-based index of the last non-empty item. You can easy convert it to A1-formatted range with ADDRESS function.

try:
=INDEX(IFNA(REGEXEXTRACT(" "&TRIM(FLATTEN(QUERY(TRANSPOSE(IF(A2:D="",,
ADDRESS(ROW(A2:A), COLUMN(A:D), 4))),,9^9))), " (.{1,3}\d+)$")))
and:
=INDEX(IFNA(SUBSTITUTE(REGEXEXTRACT(" "&TRIM(FLATTEN(QUERY(TRANSPOSE(IF(A2:D="",,
SUBSTITUTE(A2:D, " ", "♦"))),,9^9))), "((?:[^ ]+ *){1})$"), "♦", " ")))

Related

ArrayFormula For Join A Range Column

I want to use ArrayFormula for JoinText for multiple columns, From Column A to Column H. I already have Google App Script for it, and it works.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DISPOSISI");
const rowNo = sheet.getLastRow();
const colStaff1 = 1;
const colStaff8 = 8;
const colJoinStaff = 9;
// =============
// TEXT JOIN :
// =============
const cellStaff1 = sheet.getRange(rowNo,colStaff1).getA1Notation();
const cellStaff8 = sheet.getRange(rowNo,colStaff8).getA1Notation();
sheet.getRange(rowNo,colJoinStaff).setValue(sheet.getRange(rowNo,colJoinStaff).setFormula('TEXTJOIN(", ";TRUE;'+cellStaff1+':'+cellStaff8+')').getValue());`
Every I added one new Row, I want the result will appear with ArrayFormula.
This is my formula :
=arrayformula(if(row(A:A)=1;"JOIN VALUE WITH COMMA";ARRAYFORMULA(IF((A:A)="";"";ARRAYFORMULA(TEXTJOIN(", ";TRUE;(A:A):(H:H)))))))
But it does not work.
My Spreadsheet
try:
={"JOIN VALUE WITH COMMA";
INDEX(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
IF(A2:H="";;A2:H&","));;9^9))); ",$"; ))}
or maybe:
={"JOIN VALUE WITH COMMA";
INDEX(REGEXREPLACE(TRIM(FLATTEN(QUERY(TRANSPOSE(
IF(A2:H="";;A1:H1&": "&A2:H&","));;9^9))); ",$"; ))}

Google script - In the following code, instead of 'includes', how to do 'not includes'

Sample sheet
This is the code I have now, and what I want to do is also bring in all data from C where text does not include '250p' in col A.
const sS = SpreadsheetApp.getActiveSpreadsheet()
function grabData() {
const sheetIn = sS.getSheetByName('data')
const sheetOut = sS.getSheetByName('Desired Outcome')
const range = 'A2:B'
/* Grab all the data from columns A and B and filter it */
const values = sheetIn.getRange(range).getValues().filter(n => n[0])
/* Retrieve only the names if it containes 250p */
/* In format [[a], [b], ...] */
const parsedValues = values.map((arr) => {
const [type, name] = arr
if (type.toLowerCase().includes('250p')) {
return name.split('\n')
}
})
.filter(n => n)
.flat()
.map(n => [n])
/* Add the values to the Desired Outcome Sheet */
sheetOut
.getRange(sheetOut.getLastRow() + 1, 1, parsedValues.length)
.setValues(parsedValues)
}
I tried doing:
if (!type.toLowerCase().includes('250p')) {
if (type.whenTextDoesNotContain('250p')) {
But on both occasions I get that, that is not a function.
I believe your goal is as follows.
Your Spreadsheet has the values in the columns "A" and "C".
You want to check the column "A". When the value of column "A" doesn't include the text of 250p, you want to copy the values from the column "C" to the column "A" of the destination sheet. In this case, you want to split the values by \n.
Modification points:
In your script, in order to retrieve the values from the columns "A" and "C", I thought that const range = 'A2:B' should be const range = 'A2:C' + sheetIn.getLastRow();, and also const [type, name] = arr is const [type, , name] = arr.
In order to retrieve the rows that 250p is not included in the column "A", I modified your if statement to if (!type.toLowerCase().includes('250p')) {.
When these points are reflected to your script, it becomes as follows.
Modified script:
const sS = SpreadsheetApp.getActiveSpreadsheet();
function grabData() {
const sheetIn = sS.getSheetByName('data');
const sheetOut = sS.getSheetByName('Desired Outcome');
const range = 'A2:C' + sheetIn.getLastRow();
const values = sheetIn.getRange(range).getValues();
const parsedValues = values.map((arr) => {
const [type, , name] = arr;
if (!type.toLowerCase().includes('250p')) {
return name.split('\n');
}
})
.filter(n => n)
.flat()
.map(n => [n]);
sheetOut
.getRange(sheetOut.getLastRow() + 1, 1, parsedValues.length)
.setValues(parsedValues);
}
If you want to retrieve the rows that 250p is included in the column "A", please modify if (!type.toLowerCase().includes('250p')) { to if (type.toLowerCase().includes('250p')) {.
Note:
In this modified script, your provided Spreadsheet is used. So, when you change the Spreadsheet, this modified script might not be able to be used. Please be careful about this.

Fuzzy matching in Google Sheets

Trying to compare two columns in GoogleSheets with this formula in Column C:
=if(A1=B1,"","Mismatch")
Works fine, but I'm getting a lot of false positives:
A.
B
C
MARY JO
Mary Jo
JAY, TIM
TIM JAY
Mismatch
Sam Ron
Sam Ron
Mismatch
Jack *Ma
Jack MA
Mismatch
Any ideas how to work this?
This uses a score based approach to determine a match. You can determine what is/isn't a match based on that score:
Score Formula = getMatchScore(A1,B1)
Match Formula = if(C1<.7,"mismatch",)
function getMatchScore(strA, strB, ignoreCase=true) {
strA = String(strA);
strB = String(strB)
const toLowerCase = ignoreCase ? str => str.toLowerCase() : str => str;
const splitWords = str => str.split(/\b/);
let [maxLenStr, minLenStr] = strA.length > strB.length ? [strA, strB] : [strB, strA];
maxLenStr = toLowerCase(maxLenStr);
minLenStr = toLowerCase(minLenStr);
const maxLength = maxLenStr.length;
const minLength = minLenStr.length;
const lenScore = minLength / maxLength;
const orderScore = Array.from(maxLenStr).reduce(
(oldItem, nItem, index) => nItem === minLenStr[index] ? oldItem + 1 : oldItem, 0
) / maxLength;
const maxKeyWords = splitWords(maxLenStr);
const minKeyWords = splitWords(minLenStr);
const keywordScore = minKeyWords.reduce(({ score, searchWord }, nItem) => {
const newSearchWord = searchWord?.replace(new RegExp(nItem, ignoreCase ? 'i' : ''), '');
score += searchWord.length != newSearchWord.length ? 1: 0;
return { score, searchWord: newSearchWord };
}, { score: 0, searchWord: maxLenStr }).score / minKeyWords.length;
const sortedMaxLenStr = Array.from(maxKeyWords.sort().join(''));
const sortedMinLenStr = Array.from(minKeyWords.sort().join(''));
const charScore = sortedMaxLenStr.reduce((oldItem, nItem, index) => {
const surroundingChars = [sortedMinLenStr[index-1], sortedMinLenStr[index], sortedMinLenStr[index+1]]
.filter(char => char != undefined);
return surroundingChars.includes(nItem)? oldItem + 1 : oldItem
}, 0) / maxLength;
const score = (lenScore * .15) + (orderScore * .25) + (charScore * .25) + (keywordScore * .35);
return score;
}
try:
=ARRAYFORMULA(IFERROR(IF(LEN(
REGEXREPLACE(REGEXREPLACE(LOWER(A1:A), "[^a-z ]", ),
LOWER("["&B1:B&"]"), ))>0, "mismatch", )))
Implementing fuzzy matching via Google Sheets formula would be difficult. I would recommend using a custom formula for this one or a full blown script (both via Google Apps Script) if you want to populate all rows at once.
Custom Formula:
function fuzzyMatch(string1, string2) {
string1 = string1.toLowerCase()
string2 = string2.toLowerCase();
var n = -1;
for(i = 0; char = string2[i]; i++)
if (!~(n = string1.indexOf(char, n + 1)))
return 'Mismatch';
};
What this does is compare if the 2nd string's characters order is found in the same order as the first string. See sample data below for the case where it will return mismatch.
Output:
Note:
Last row is a mismatch as 2nd string have r in it that isn't found at the first string thus correct order is not met.
If this didn't meet your test cases, add a more definitive list that will show the expected output of the formula/function so this can be adjusted, or see player0's answer which solely uses Google Sheets formula and is less stricter with the conditions.
Reference:
https://stackoverflow.com/a/15252131/17842569
The main limitation of traditional fuzzy matching is that it doesn’t take into consideration similarities outside of the strings. Topic clustering requires semantic understanding. Goodlookup is a smart function for spreadsheet users that gets very close to semantic understanding. It’s a pre-trained model that has the intuition of GPT-3 and the join capabilities of fuzzy matching. Use it like vlookup or index match to speed up your topic clustering work in google sheets.
https://www.goodlookup.com/

Get last row ignoring formula in google script

I used a google script that move row from one sheet to another from column A to column D. On column E i have a formula. How can i put data on last row from Column A to D from example, ignoring formula from column E. Can someone help me with this?
function onEdit(e){
let r = e.range;
if (r.columnStart != 4 || r.rowStart == 1 || e.value == null) return;
const sh = SpreadsheetApp.getActive();
const valArray = ["Waiting","In progress","Complete"];
const destArray = ["Order","Open order","Complete order"];
let dest = sh.getSheetByName(destArray[valArray.indexOf(e.value)]);
let src = sh.getActiveSheet();
if (dest.getName() == src.getName()) return;
src.getRange(r.rowStart,1,1,4).moveTo(dest.getRange(dest.getLastRow()+1,1,1,4));
src.deleteRow(r.rowStart);
}
Here an example of my problem, maybe this will explain better:https://docs.google.com/spreadsheets/d/1qowADCPYiyej25ezXtjVLO5fvg9Gr9rolX3bh2-ZAG4/edit#gid=0
Replace dest.getLastRow() by dest.getLastDataRow('A') and add this function:
Object.prototype.getLastDataRow = function(col){
var lastRow = this.getLastRow();
var range = this.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
I still sugest you to erase all formulas in column E and put in E1
={"price * 2";ARRAYFORMULA(if(C2:C>0;C2:C*2;""))}
so that the formula will populate / will be active the new row

Split row in multiple other rows in Power Bi based on a division of a number

In Power BI Desktop i have a table from an excel file and i want to split a row based on a division between the value of a specific column and a default number.
In more details lets assume tha we have a table like this :
if the default value we want to devide column Amount is 50,then the desirable result would be something like that :
Do you have any idea how can i implement that in Power query editor or with dax?
Thanks
Tested this in Power Query for Excel, but hopefully should work for you in Power BI too. If you create a function like:
divisionToList = (numberToDivide as number, numberToDivideBy as number) as list =>
let
divisionResult = numberToDivide / numberToDivideBy,
isResultValid = (divisionResult >= 0) and (Number.Mod(divisionResult, 1) = 0),
errorIfInvalid = Error.Record("Cannot create a list with " & Text.From(divisionResult) & " items", Number.ToText(numberToDivide) & " / " & Number.ToText(numberToDivideBy) & " = " & Text.From(divisionResult), null),
listOrError = if isResultValid then List.Repeat({divisionResult}, divisionResult) else error errorIfInvalid
in listOrError,
It should divide two numbers and return a list of length d in which each element is d (d is the result of the division). This list can then, in the context of a table, be expanded into new rows.
There is some basic error handling in the function for cases where the division yields a problematic number (since you can't have a list with, for example, 5.1 elements or -1 elements). You can change/remove this handling if necessary.
I think this code below takes me from your first image to your second image -- and hopefully will give you some idea on how to go about achieving this.
let
mockData = Table.FromColumns({{200, 400}, {"A", "B"}}, type table [Amount = number, Description = text]),
defaultValue = 50, // Not sure what logic is required for arriving at this figure, so have simply assigned it.
divisionToList = (numberToDivide as number, numberToDivideBy as number) as list =>
let
divisionResult = numberToDivide / numberToDivideBy,
isResultValid = (divisionResult >= 0) and (Number.Mod(divisionResult, 1) = 0),
errorIfInvalid = Error.Record("Cannot create a list with " & Text.From(divisionResult) & " items", Number.ToText(numberToDivide) & " / " & Number.ToText(numberToDivideBy) & " = " & Text.From(divisionResult), null),
listOrError = if isResultValid then List.Repeat({divisionResult}, divisionResult) else error errorIfInvalid
in listOrError,
invokeFunction = Table.TransformColumns(mockData, {{"Amount", each divisionToList(_, defaultValue), type list}}),
expanded = Table.ExpandListColumn(invokeFunction, "Amount")
in
expanded