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
Related
I have a sheet with 4 columns, as shown below:
1
Date
Item Name
Counter
Flag
3
Date 1
Item A
1
4
Date 1
Item B
1
5
Date 2
Item B
2
6
Date 3
Item A
2
1
7
Date 3
Item B
3
8
Date 4
Item A
1
9
Date 5
Item A
2
Currently, I'm using a countif function [=countif(B$2:B2,B2)] to count the number of times a specific item appears in the spreadsheet. However, I need to find a way to restart the counter if there is a 1 in column D. In this case, this would mean that the formula in row 8 column C would be [=COUNTIF(B$8:B8,B8)] and would continue counting until it finds another row with a 1 in column D (e.g., formula in column C row 9 would be =COUNTIF(B$8:B9,B9). It would also ideally check whether there is a prior row with a 1 in column D, not through the order of the sheet, but by checking that it's date is smaller (and yet the closest date with a 1 in column D).
I've written the following script, which sets the row with a 1 in column D to 0 and sets the countif for the starting rows correctly to [=countif(B$2:B2,B2)], but it sets any row after there is a row with a 1 in column D as the same formula, with the starting range at B$2.
function setCountifFormula() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var data = sheet.getDataRange().getValues();
for (var i = 1; i < data.length; i++) { //iterate through each row
var colBValue = data[i][1]; //get columnB in i
var colAValue = data[i][0]; // get date in i
var colDValue = data[i][3]; // get flag in i
var closestRow = 1; // empty variable
if( colDValue == "1") { //if columnD = 1
sheet.getRange(i+1,3).setValue(0); // set columnC = 0
} else {
for (var j = 1; j < data.length; j++) { //iterate through other rows
if (data[j][1] === colBValue && data[j][3] === "1") { // if columnB in j = ColumnB in i, and flag in row j = 1
var dateToCompare = data[j][0]; //set datetoCompare = date in row j
closestRow = j;
if (dateToCompare < colAValue) {
var range = "B$" + (closestRow + 1) + ":B" + (i + 1);
var formula = "=COUNTIF(" + range + ",B" + (i + 1) + ")";
sheet.getRange(i + 1, 3).setFormula(formula);
} else {
var range = "B$2:B" + (i+1);
var formula = "=COUNTIF(" + range + ",B" + (i+1) + ")";
sheet.getRange(i+1, 3).setFormula(formula);
}
}
}
if (closestRow === 1) {
var range = "B$2:B" +(i+1);
var formula = "=COUNTIF("+range +",B"+(i+1)+")";
sheet.getRange(i+1,3).setFormula(formula);
}
}
}
}
I can post the spreadsheet if needs be. If there is a different way without using scripts or COUNTIF, it'd be appreciated. Thanks!
I'm much better at scripting than complex formulas so here is an example of how I would do it.
function myCountif() {
try {
let values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
values.shift(); // remove headers
let unique = values.map( row => row[1] );
unique = [...new Set(unique)];
let count = unique.map( row => 0 );
let counts = values.map( row => 0 );
values.forEach( (row,rIndex) => {
let cIndex = unique.findIndex( item => item === row[1] );
count[cIndex] = count[cIndex]+1;
counts[rIndex] = count[cIndex];
if( row[3] === 1 ) count[cIndex] = 0;
}
)
return counts;
}
catch(err) {
console.log(err);
}
}
Reference
Array.shift()
Array.map()
Set Object
Array.forEach()
Array.findIndex()
Arrow function =>
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++);
}
});
}
I feel I have this script almost working as intended, but I am unsure as to where to place certain components of the procedure within the for loop to achieve the desired result. Tunnel vision is very much in effect right now, it's entirely possible I am overthinking a simple task. Please let me know if I can describe the issue more clearly. Any suggestions or pointers towards an existing resource are helpful.
Setup: Sheet one contains a dynamic vertical list of text values starting in cell I3 going down. Sheet two contains a dynamic vertical list of text values starting in range A2 going down, and has a similar set of text values in the same rows in column B.
Goal:
Get the value of each used cell in Sheet1 column I (range one)
Get the value of each used cell in Sheet2 column A (range two)
For each used cell in range one, check the value of each range one
cell to see if the value exists in range two
If a match exists, get the row number of the cell in range two that
contains the value (there will only ever be a single match if a match
exists)
Get the value of the cell on that same row in Sheet2 column B
Set the above value as the cell value in the row containing the value
found in both ranges on Sheet1 Column M
Below is what I have been able to come up with. I am able to get it to function up to the last step. It seems the matching rowNumber variable is not updating with each for loop.
// Get values of range one
var lastRowRangeOne = sheetone.getRange('I3').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var rangeOneValues = sheetone.getRange('I3:I' + lastRowClientColumn).getValues();
// Get values of range two
var sheettwo = spreadsheet.getSheetByName('Sheet2');
var sheettwoLastRow = sheettwo.getLastRow();
var sheettwoList = sheettwo.getRange('A2:A' + sheettwoLastRow).getValues();
var sheettwoListFlat = sheettwoList.map(function(row) {return row[0];});
for (var i = 0; i< rangeOneValues.length ; i++){ // for each row in range one
if (sheettwoListFlat[i] == rangeOneValues[i]) { // if the range one value exists in range two
var rowNumber = sheettwoListFlat.indexOf(rangeOneValues[i]) + 3; // get the row number of the matching value found in range two
var sheetOneColumnMValue = sheettwo.getRange('B' + rowNumber).getValue(); // get the cell value of the same row in sheet two column B
var sheetOneRowNumber = i + 3; // get the row number of the range one value
sheetone.getRange('M' + sheetOneRowNumber).setValue(sheetOneColumnMValue); // set the cell value of sheet one column M row x, where x is sheetOneRowNumber
}
}
If you print the value of sheettwoListFlat, it has only 5 elements. Once the iterator of for loop reaches 5 or more it will automatically set if (sheettwoListFlat[i] == rangeOneValues[i]) { to false. Also the statement will only work if the elements of the same index of both arrays are equal.
It would be also more efficient if you search the Sheet1 using the values of Sheet2 since Sheet2 has lesser value.
Here I used TextFinder to find the text within a range.
Using Sheet2 values as search key
Code:
function myFunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetone = spreadsheet.getSheetByName('Sheet1');
var lastRowRangeOne = sheetone.getRange('I2').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var rangeOneValues = sheetone.getRange('I2:I' + lastRowRangeOne);
var sheettwo = spreadsheet.getSheetByName('Sheet2');
var sheettwoLastRow = sheettwo.getLastRow();
var sheettwoList = sheettwo.getRange('A2:B' + sheettwoLastRow).getValues();
for (var i = 0; i < sheettwoList.length; i++){
var find = rangeOneValues.createTextFinder(sheettwoList[i][0]).findNext()
if(find){
var sheetOneRowNumber = find.getRow();
sheetone.getRange('M' + sheetOneRowNumber).setValue(sheettwoList[i][1]);
}
}
}
Using Sheet1 values as search key
Code:
function myFunction() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetone = spreadsheet.getSheetByName('Sheet1');
var lastRowRangeOne = sheetone.getRange('I2').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
var rangeOneValues = sheetone.getRange('I2:I' + lastRowRangeOne).getValues();
var sheettwo = spreadsheet.getSheetByName('Sheet2');
var sheettwoLastRow = sheettwo.getLastRow();
var sheettwoList = sheettwo.getRange('A2:B' + sheettwoLastRow);
for (var i = 0; i< rangeOneValues.length ; i++){ // for each row in range one
var find = sheettwoList.createTextFinder(rangeOneValues[i][0]).findNext();
if(find){
var rowNumber = find.getRow()
var sheetOneColumnMValue = sheettwo.getRange('B' + rowNumber).getValue(); // get the cell value of the same row in sheet two column B
var sheetOneRowNumber = i + 2;
sheetone.getRange('M' + sheetOneRowNumber).setValue(sheetOneColumnMValue);
}
}
}
Output:
Note: Both produce the same output but using Sheet2 values as search key is more faster than the other.
function compareTwoCols() {
const c1 = 'COL1';//column names
const c2 = 'COL3';
const ss1 = SpreadsheetApp.openById(gobj.globals.datagenid);//data set 1
const sh1 = ss1.getSheetByName('Sheet1');
const [hd1, ...vs1] = sh1.getDataRange().getValues();
let col1 = {};
hd1.forEach((h, i) => { col1[h] = i });
const ds1 = vs1.map((r, i) => {
return r[col1[c1]];
});
const ss2 = SpreadsheetApp.openById(gobj.globals.ssid);//data set 2
const sh2 = ss2.getSheetByName('Sheet0');
const [hd2, ...vs2] = sh2.getDataRange().getValues();
let col2 = {};
hd2.forEach((h, i) => { col2[h] = i });
const ds2 = vs2.map((r, i) => {
return r[col2[c2]]
});
let matches = { pA: [] };
let idx = -1;
ds1.forEach((e, i) => {
let from = '';
do {
idx = ds2.indexOf(e, from);
if (~idx) {
if (!matches.hasOwnProperty(e)) {
matches[e] = [];
matches[e].push({ val1: e, row1: i + 2, col1: col1[c1] + 1, row2: idx + 2, col2: col2[c2] +1 });
matches.pA.push(e);
} else {
matches[e].push({ val1: e, row1: i + 2, col1: col1[c1] + 1, row2: idx + 2, col2: col2[c2] + 1});
}
from = idx + 1;
}
} while (~idx);
});
Logger.log(JSON.stringify(matches));
}
Spreadsheet1 Sheet1:
COL1
COL2
COL3
COL4
COL5
3
4
2
3
4
2
6
6
1
4
5
1
7
5
5
9
8
7
9
5
7
9
0
8
1
8
2
8
7
9
5
8
7
9
9
1
2
0
8
6
2
7
4
0
3
8
2
0
2
6
Spreadsheet2 Sheet0:
COL1
COL2
COL3
COL4
COL5
5
1
2
7
6
4
5
7
8
2
6
3
8
1
5
0
7
6
3
6
4
7
6
1
7
5
6
9
2
1
3
0
2
2
8
4
5
0
8
1
1
3
9
2
2
3
6
7
0
3
Matches Object:
{
"2":[
{
"val1":2,
"row1":3,//ds1 row 3
"col1":1,
"row2":2,//ds2 row 4
"col2":3
},
{
"val1":2,
"row1":3,//ds1 row 3
"col1":1,
"row2":8,//ds2 row 8
"col2":3
},
{
"val1":2,
"row1":10,
"col1":1,
"row2":2,
"col2":3
},
{
"val1":2,
"row1":10,
"col1":1,
"row2":8,
"col2":3
}
],
"7":[
{
"val1":7,
"row1":6,
"col1":1,
"row2":3,
"col2":3
},
{
"val1":7,
"row1":6,
"col1":1,
"row2":11,
"col2":3
}
],
"8":[
{
"val1":8,
"row1":7,
"col1":1,
"row2":4,
"col2":3
},
{
"val1":8,
"row1":11,
"col1":1,
"row2":4,
"col2":3
}
],
"9":[
{
"val1":9,
"row1":5,
"col1":1,
"row2":7,
"col2":3
},
{
"val1":9,
"row1":5,
"col1":1,
"row2":10,
"col2":3
}
],
"pA":[//just an array of all of the matches
2,
9,
7,
8
]
}
I have the following google script that will run against 500+ rows of data in the "InputFromHospital" sheet and in the middle of execution, getting the "Exceeded maximum execution time" error.
I was having few more lines of code but after reading the similar questions in StackOverflow, I removed and kept only the needed lines/source code. I am not sure how I could further optimize.
Looking for your suggestions/expertise to optimize and to reduce the execution time to under 5 minutes?
function feedToMasterAndPolice() {
var ssbook = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ssbook.getSheetByName('MasterPatientData')
var sLastRow = srcSheet.getLastRow() + 1
var inputSheet = ssbook.getSheetByName('InputFromHospital')
var iLastRow = inputSheet.getLastRow();
var pSheet = ssbook.getSheetByName('DataToPolice')
var pLastRow = pSheet.getLastRow() + 1;
var todayDate = Utilities.formatDate(new Date(), "IST", "dd/MMM/yyyy")
//Loop thru all rows in "Input.." sheet
for (var i = 2; i <= iLastRow; i++) {
//Reading contact number from "Input.." sheet
var value = inputSheet.getRange(i, 5).getValue();
var gID = "";
if (value.toString.length > 0) {
//Generating unique patient ID for each COVID Patient
gID = "PID".concat(srcSheet.getLastRow());
srcSheet.getRange(sLastRow, 1).setValue(gID.toString());
//Looping thru all 8 colums in "Input" sheet
for (var colN = 2; colN < 9; colN++) {
var actCellVal = inputSheet.getRange(i, colN).getValue();
//Set value for contact# and date values
if (colN == 5 || colN == 6 || colN == 8) {
srcSheet.getRange(sLastRow, colN).setValue(actCellVal)
} else {
//All String values - upper case
srcSheet.getRange(sLastRow, colN).setValue(actCellVal.toString().toUpperCase())
}
}
var cR = srcSheet.getLastRow();
//Adding formula to calculate "DAYS SINCE ADMISSION" - example =if(A2<>"",TODAY()-F2,"")
srcSheet.getRange(sLastRow, 9).setFormula("=if(A" + cR + "<>\"\",TODAY()-F" + cR + ",\"\")")
//Adding formula to calculate "FOLLOW UP NEEDED" - example =if(I2<=Admin!$C$2,"YES","NO"
srcSheet.getRange(sLastRow, 10).setFormula("=if(I" + cR + "<=\'Admin\'!$C$2,\"YES\",\"NO\")");
//Add current date
srcSheet.getRange(sLastRow, 11).setValue(todayDate);
sLastRow = sLastRow + 1
} else {
//Above logic same when contact number is blank
//Not considerd Patient ID #
var ppID = "NCPID".concat(pSheet.getLastRow());
//Untested
pSheet.getRange(pLastRow, 1).clear();
pSheet.getRange(pLastRow, 1).setValue(ppID.toString());
//till above
for (var colN = 2; colN <= 9; colN++) {
var actCellVal = inputSheet.getRange(i, colN).getValue();
if (colN == 6 || colN == 8) {
pSheet.getRange(pLastRow, colN).setValue(actCellVal)
} else {
pSheet.getRange(pLastRow, colN).setValue(actCellVal.toString().toUpperCase())
pSheet.getRange(pLastRow, 9).setValue(todayDate);
}
}
pLastRow = pLastRow + 1
}
}
//this.ClearAnySheet("InputFromHospital")
};
Sample file available here.
I believe your goal as follows.
You want to reduce the process cost of your script.
Modified script:
function feedToMasterAndPolice_b() {
var ssbook = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ssbook.getSheetByName('MasterPatientData');
var sLastRow = srcSheet.getLastRow() + 1;
var inputSheet = ssbook.getSheetByName('InputFromHospital');
var pSheet = ssbook.getSheetByName('DataToPolice');
var pLastRow = pSheet.getLastRow() + 1;
var todayDate = Utilities.formatDate(new Date(), "IST", "dd/MMM/yyyy");
var [, ...values] = inputSheet.getDataRange().getValues();
var { masterPatientData, dataToPolice } = values.reduce((o, [c1, c2, c3, c4, c5, c6, c7, c8], i) => {
if (c5.toString() != "") {
var last = sLastRow - 1 + o.masterPatientData.length;
o.masterPatientData.push([
`PID${last}`,
...[c2, c3, c4, c5, c6, c7, c8].map((c, j) => [3, 4, 6].includes(j) ? c : c.toString().toUpperCase()),
`=if(A${last + 1}<>"",TODAY()-F${last + 1},"")`,
`=if(I${last + 1}<='Admin'!\$C\$2,"YES","NO")`,
todayDate,
]);
} else {
var last = pLastRow - 1 + o.dataToPolice.length;
o.dataToPolice.push([
`NCPID${last}`,
...[c2, c3, c4, c5, c6, c7, c8].map((c, j) => [4, 6].includes(j) ? c : c.toString().toUpperCase()),
todayDate,
]);
}
return o;
}, { masterPatientData: [], dataToPolice: [] });
if (masterPatientData.length > 0) {
srcSheet.getRange(sLastRow, 1, masterPatientData.length, masterPatientData[0].length).setValues(masterPatientData);
}
if (dataToPolice.length > 0) {
pSheet.getRange(pLastRow, 1, dataToPolice.length, dataToPolice[0].length).setValues(dataToPolice);
}
}
Note:
Unfortunately, I cannot test above modified script with your actual situation. So when my proposed script cannot be used for your actual situation, can you provide the sample Spreadsheet for replicating the issue? By this, I would like to confirm it.
References:
Benchmark: Reading and Writing Spreadsheet using Google Apps Script
getValues()
setValues(values)
I have Google Sheets just like this below:
Column1 Column2
444 111
444 222
444 333
999 111
999 222
888 111
I need to get return data in Column2 as variable: 111 return as var1, 222 return as var2 and so on, if the same value found in Column1.
I already try some of couple loop to get the variable:
function certificate(e) {
var sh = SpreadsheetApp.getActive().getSheetByName('sheetname'); //define the sheet name
var rg = sh.getDataRange();
var rows = rg.getLastRow(); //Get the number of last line
var values = rg.getValues(); //Put the data in array
var matchline;
for (var i = 1; i < rows; i++) {
var targetmr = values[i][0];
for (var m = 2; m < rows; m++) {
var targetmr2 = values[m][1];
if (targetmr == targetmr2 ) {
if (targetmr == values[i-1][0]) {
matchline++;
//Logger.log(targetmr + "is match" + matchline + "row"); //second and subsequent is in here
sh2.getRange(matchline+1,10).setValue(values[i][1]);
sh2.getRange(matchline+1,11).setValue(values[i][2]);
sh2.getRange(matchline+1,12).setValue(values[i][3]);
} else {
//Logger.log(targetmr + "is match" + m + "row");
matchline = m; //the same quotation number is in here
sh2.getRange(m+1,10).setValue(values[i][1]);
sh2.getRange(m+1,11).setValue(values[i][2]);
sh2.getRange(m+1,12).setValue(values[i][3]);
}
break; //Process only the first match
}
}
}
}
What code should I use to do this in Google Apps Script?
Help would be appreciated,
Sincerely
Ruhul