Partial Match Not Working in Google Sheets - google-apps-script

I have this sheet tab that contains these columns:
In another tab, I have this column:
I want the result to be like this:
I have this formula that I'm using that somewhat works:
=IFERROR(TEXTJOIN(" ",TRUE,QUERY(ARRAYFORMULA(IF(REGEXMATCH($B2,Masterlist!$A$2:$A$139),Masterlist!$A$2:$A$139,"")),"where Col1 is not null")))
but the result I'm getting is like this:

function lfunko() {
const ss = SpreadsheetApp.getActive();
const sh1 = ss.getSheetByName("Sheet1");
const [h1, ...vs1] = sh1.getDataRange().getDisplayValues();
let obj1 = {};
h1.forEach((h, i) => { obj1[h] = i + 1; });
const code = sh1.getRange(2,obj1["Activity Code"],sh1.getLastRow() - 1).getDisplayValues().flat().map(c => c.replace(/ /g,''));
const sh2 = ss.getSheetByName("Sheet2");
const [h2, ...vs2] = sh2.getDataRange().getDisplayValues();
let obj2 = {};
h2.forEach((h, i) => { obj2[h] = i + 1; });
const acct = sh2.getRange(2,obj2["Account"],sh2.getLastRow() - 1).getDisplayValues().flat();
let arr = [["Activity Code","Account"]]
acct.forEach((a,i) =>
code.forEach(c => {
if(~c.indexOf(a)) {
arr.push([c,a]);
}
}
));
Logger.log(JSON.stringify(arr));
const sh3 = ss.getSheetByName("Sheet3");
sh3.clearContents();
sh3.getRange(1,1,arr.length, arr[0].length).setValues(arr);
}
Sheet1:
A
1
Activity Code
2
_account1_immediate
3
_lunch_account2
4
_break_account3_lunch
5
_break account 3
Sheet2:
A
1
Account
2
account1
3
account2
4
account3
5
account11
Sheet3:
A
B
1
Activity Code
Account
2
_account1_immediate
account1
3
_lunch_account2
account2
4
_break_account3_lunch
account3
5
_breakaccount3
account3

Related

Check if column A is equal today's date and get the value of column B

I have a Google Sheet with a table like this:
Data
On Duty
Support
15/02/2023
Name1
Name4
16/02/2023
Name2
Name5
17/02/2023
Name3
Name6
I need to check the column A if the date is equal with today's date and get the value of column B and C and send the values to a slack channel.
I tried this but isn't working:
function sendSlackMessage() {
const onduty = QUERY("SELECT B WHERE todate(A)=date'" & text(today(), "dd/MM/yyyy") &"'");
const support = QUERY("SELECT C WHERE todate(A)=date'" & text(today(), "dd/MM/yyyy") &"'");
const url = "https://hooks.slack.com.services/xxxxxx/xxxxxxx/xxxxxxxxxxxxxxxxxxxxxx";
const params = {
method: "post",
contentType: "application/json",
payload: JSON.stringfy({
"text" : "Analyst on duty today: " + onduty + "\n" + "Support analyst: " + support
})
}
const sendMsg = UrlFetchApp.fetch(url, params);
var respCode = sendMsg.getResponseCode();
Logger.log(sendMsg);
Logger.log(respCode);
}
Here's how you can get the values:
const dt = new Date();
const dtv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(2,1,sh.getLastRow() - 1, 2).getValues();
let vo = vs.map(r => {
let d = new Date(r[0]);
let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf();
if(dtv == dv) {
return r[1];
} else {return null;}
}).filter(e => e);

Sum two columns and display on third column

I want to sum two columns that have numerical values and display the results on the third column. I have the script below but that deletes the two columns and display the results on the first column.
Any suggestions?
function test2() {
var ss = SpreadsheetApp.getActiveSheet();
var rng = ss.getRange('E2:F6');
var val = rng.getValues();
for(row=0;row<val.length;row++) {
val[row][0] =val[row][0]+val[row][1];;
val[row][1] = '';
}
rng.setValues(val)
}
Try this instead. Just get the colum G and overwrite anything in it with the sum.
function test() {
var ss = SpreadsheetApp.getActiveSheet();
var rng = ss.getRange('E2:G6'); // just get the extra column
var val = rng.getValues();
for(row=0;row<val.length;row++) {
val[row][2] = val[row][0]+val[row][1];
val[row][1] = '';
}
rng.setValues(val)
}
Using map:
function one() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const vs = sh.getRange('E2:F6').getValues().map(r => [r[0],r[1],r[0]+r[1]]);
sh.getRange(2,5,vs.length,vs[0].length).setValues(vs);
}
E
F
G
1
COL5
COL6
2
5
6
11
3
6
7
13
4
7
8
15
5
8
9
17
6
9
10
19
Array.map

Deleting rows in bulk google sheets using Google Apps Script

I have a bulk sheet(more that 10000 rows) and 5 filters. I have to delete row if any of 5 columns is TRUE.
Delete row with five "TRUE"
function lfunko2() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Leads");
const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();//row 2 is start of data
let d = 0;
vs.forEach((r, i) => {
if(r.filter(c => c == "TRUE").length > 4) {
sh.deleteRow(i + 2 - d++);
}
});
}

Google Apps Script to filter unique data in a filtered range

I have a data similar to this one
Screenshot of data
First, I filtered out the range where column N is marked as yes and I success to get that. Then I want to filter out all unique value in column A together with value in column M and return the row number of the unique value. Please help me with that one.
So the result will be a range/array like this:
[[HCMC 1, handoi3648#gmail.com, 1 (/// row 1 of the filtered range)],
[HCMC 4, handoi3648#gmail.com, 3 (/// row 3 of the filtered range)],
[HCMC 5, handoi3648#gmail.com, 4 (/// row 4 of the filtered range)]]
My code upto the first filtered range as follows
function HouseLeaseReminderAtYE(){
var SS = SpreadsheetApp.getActiveSpreadsheet();
var Sheet = SS.getSheetByName("Tax_Master");
var Range = Sheet.getDataRange();
var Values = Range.getDisplayValues();
var hl_to_remind_at_final_range = Values.filter(function(item){return item[13]==="Y"});}
function HouseLeaseReminderAtYE() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh = ss.getSheetByName("Tax_Master");
const rg = sh.getDataRange();
const vs = rg.getDisplayValues();
let fvs = vs.filter(function (item) { return item[13] == "Y" });
let uO = {pA:[]};
fvs.forEach((r,i) => {
if(!uO.hasOwnProperty(r[0])) {
uO[r[0]] = [r[0],r[12],i+1];
uO.pA.push(r[0]);
}
});
let uA = uO.pA.map( p => [uO[p]]);
Logger.log(uA);
}

Google Sheets: split cells vertically and copy surrounding row entries

I need to do the following to optimize this sheet:
split each multi-line cell in column B so each email address will appear on a new row inserted under the original row.
Copy the data from cells in column A on the original row
I tried split+transpose formulas and a script I found here, but that is returning an error.
Here is the script:
function split_rows2(anArray) {
var res = anArray.reduce(function(ar, e) {
var splitted = e.slice(3, 12).map(function(f) {return f.toString().split(",").map(function(g) {return g.trim()})});
var temp = [];
for (var j = 0; j < splitted[0].length; j++) {
temp.push(e.slice(0, 3).concat(splitted.map(function(f) {return f[j] ? (isNaN(f[j]) ? f[j] : Number(f[j])) : ""})).concat(e.slice(12, 20)));
}
Array.prototype.push.apply(ar, temp);
return ar;
}, []);
return res;
}
=ArrayFormula(QUERY(SPLIT(FLATTEN({A2:A&"♦"&SPLIT(B2:B,",")}),"♦"),"select * where Col2 is not null",0))
Before:
Col1
Col2
A
1,2,3
B
4,5
After:
Col1
Col2
A
1
A
2
A
3
B
4
B
5
A multiline cell implies that they are delimited by line feeds '\n' not commas
function myfunk() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
let vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).clearContent();
vs.forEach((r, i) => {
let t = r[1].toString().split('\n');
if (t.length > 1) {
t.forEach((e, j) => {
if(j == 0) {
r[1] = e;
} else {
let arr = Array.from(r, x => '');
arr[1] = e;
vs.splice(i + j, 0 , arr)
}
});
}
});
sh.getRange(2, 1, vs.length, vs[0].length).setValues(vs);
}
Sheet 0 after:
COL1
COL2
COL3
COL4
0
4
19
17
5
8
10
7
0
a
21
19
b
c
9
14
17
0