Printing rows with certain text in column 2 with app script - google-apps-script

I need to print off certain rows in a google sheet depending on what is in column 2 of that row. I know how to find the rows with a for loop but the rest eludes me. Perhaps my googling skills are rusty.
This is what I have.
var app = SpreadsheetApp;
var rows = app.getActive().getSheetByName("Sheet").getMaxRows().toString();
var rows = rows.replace(".0","");
function findRows(){
for (var counter = 1; counter <= rows; counter = counter+1){
if(app.getActive().getSheetByName("Sheet").getRange(counter, 2) == "example" || "example2"){
}
}

Find the correct rows
function findrows() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const osh = sh.getSheetByName("Sheet1");
const vs = sh.getDataRange().getValues();
let s = vs.map(r => {
if(r[1] == "Example" || r[1] == "example2") {
return r;
}
}).filter(e => e);
Logger.log(JSON.stringify(s));
//you can output to a sheet with something like
//sheet.getRange(1,1,s.length,s[0].length).setValues(s);
osh.getRange(1,1,s.length,s[0].length).setValues(s);//put on another sheet
}
Execution log
4:56:34 PM Notice Execution started
4:56:35 PM Info [[2,"Example",4,5],[5,"Example",7,8],[9,"Example",11,12],[12,"Example",14,15]]
4:56:35 PM Notice Execution completed
Data:
COL1
COL2
COL3
COL4
1
2
3
4
2
Example
4
5
3
4
5
6
4
5
6
7
5
Example
7
8
6
7
8
9
7
8
9
10
8
9
10
11
9
Example
11
12
10
11
12
13
11
12
13
14
12
Example
14
15
13
14
15
16
BTW Printing is not easily done from Javascript or Google Apps Script

Related

Google Sheets - Script to move columns given the column header

Given this table schema:
Col_France
Col_Argentina
Col_Croatia
Col_Morocco
x
x
x
x
x
x
x
x
I want to create a Google Script that rearranges the columns so the order is always:
Col_Argentina -> Column 1
Col_France -> Column 2
Col_Croatia -> Column 3
Col_Morocco -> Column 4
Because the original column orders of the given table is not always as described above, I cannot simply use:
var sheet = SpreadsheetApp.getActiveSheet();
// Selects Col_France.
var columnSpec = sheet.getRange("A1");
sheet.moveColumns(columnSpec, 2);
and so on... In other words, the table schema can possibly be:
Col_Morocco
Col_Croatia
Col_France
Col_Argentina
x
x
x
x
x
x
x
x
but the desired outcome should always be the defined above. The script should be scalable. In the future, more than 4 columns should be rearranged.
My approach would be:
Define the range of columns to rearrange (they are all together)
For the first column, get the value of the column header
Depending on the value, move the column to a predefined index
Move to the next column and repeat
Iterate until end of range
Can somebody please point me to the required functions?
In your situation, when moveColumns is used, how about the following sample script?
Sample script:
function myFunction() {
var order = ["Col_Argentina", "Col_France", "Col_Croatia", "Col_Morocco"]; // This is from your question.
var sheet = SpreadsheetApp.getActiveSheet();
var obj = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0].reduce((ar, h, i) => [...ar, { from: i + 1, to: order.indexOf(h) + 1 }], []).sort((a, b) => a.to > b.to ? 1 : -1);
for (var i = 0; i < obj.length; i++) {
if (obj[i].from != obj[i].to) {
sheet.moveColumns(sheet.getRange(1, obj[i].from), obj[i].to);
obj.forEach((e, j) => {
if (e.from < obj[i].from) obj[j].from += 1;
});
}
}
}
When this script is run, the columns are rearranged by order you give. In this case, the text and cell format are also moved.
When moveColumns(columnSpec, destinationIndex) is used, the indexes of columns are changed after moveColumns(columnSpec, destinationIndex) was run. So, please be careful about this. In the above script, the changed indexes are considered.
References:
moveColumns(columnSpec, destinationIndex)
reduce()
forEach()
Order Columns:
function ordercols() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const [h,...vs] = sh.getDataRange().getValues();
const idx = {};
h.forEach((h,i) => idx[h]=i);
const o = vs.map(r => [r[idx['COL4']],r[idx['COL3']],r[idx['COL2']],r[idx['COL1']]]);
sh.clearContents();
o.unshift(['COL4','COL3','COL2','COL1']);
sh.getRange(1,1,o.length,o[0].length).setValues(o);
}
Data:
COL1
COL2
COL3
COL4
24
5
2
9
16
0
13
18
22
24
23
16
12
12
4
17
6
20
17
14
7
13
4
2
2
20
4
22
3
5
3
4
16
5
7
23
ReOrdered:
COL4
COL3
COL2
COL1
9
2
5
24
18
13
0
16
16
23
24
22
17
4
12
12
14
17
20
6
2
4
13
7
22
4
20
2
4
3
5
3
23
7
5
16

Value of 0 is being flagged as an empty cell in Apps Script, how do I get 0 to not be flagged?

After combing several cells into an array, I am checking over that array to confirm that the User has not missed any inputs through a quick check for empty cells. It works beautifully every time, unless some of those values are the number 0. When any input is 0, it triggers the flag asking the User to enter a value into each cell. 0 should be an acceptable value for the purposes of this tool, so I want to allow that while not allowing missed (blank) cells. I've been digging through documentation from Apps Script and searching StackOverflow for similar issues, but I've come up blank so far.
Below is the code I have for this function.
console.log(sourceVals)
const anyEmptyCell = sourceVals.findIndex(cell => cell == "");
if(anyEmptyCell !== -1){
const ui = SpreadsheetApp.getUi();
ui.alert(
"Input Incomplete",
"Please enter a value in ALL input cells before submitting",
ui.ButtonSet.OK
);
return;
Just change the comparison operator:
function testie() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange("A1:A23").getValues().flat();
const anyEmptyCell = vs.findIndex(cell => cell === "");//just change the comparison operator
if (anyEmptyCell !== -1) {
const ui = SpreadsheetApp.getUi();
ui.alert(
"Input Incomplete",
"Please enter a value in ALL input cells before submitting",
ui.ButtonSet.OK
);
}
}
My Data:
A
1
COL1
2
17
3
14
4
1
5
19
6
14
7
1
8
11
9
3
10
16
11
0
12
19
13
8
14
2
15
15
16
10
17
19
18
12
19
2
20
1
21
11
22
23
It finds an empty cell at index 21 which is row 22 and there is a zero in the column

How can I build an array of column D where column A = Arg

"Entries" is a sheet that I enter individual tasks with the date, name, start time, end time. It calculates total time per task
"Days" is a generated sheet that gets dates from Entries and calculates total time worked on all tasks for each unique day in Entries
I would like Days to have a column that uses the date from column A, looks in Entries, and returns the earliest start time from any row in Days that is for that date.
function startTime(theDate) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ent = ss.getSheetByName("Entries");
ent.activate();
var rng = ent.getRange(rownumber, 1, 1, numberofcolums)
var rangeArray = rng.getValues();
}
calling using :
=startTime(A1)
where A1 contains the Date that I want the check-in time for.
The array of columnA where A = 'Arg'
function colDWhereAisArg() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
let o = sh.getRange(2,1,sh.getLastRow() - 1,4).getValues().map(r => {
if(r[0] == 'Arg') {
return [r[3]];
}
}).filter(e => e);
Logger.log(JSON.stringify(o));
}
Execution log
4:29:59 PM Notice Execution started
4:30:00 PM Info [[5],[8],[13]]
4:30:00 PM Notice Execution completed
My Data:
COL1
COL2
COL3
COL4
1
2
3
4
Arg
3
4
5
3
4
5
6
4
5
6
7
Arg
6
7
8
6
7
8
9
7
8
9
10
8
9
10
11
9
10
11
12
Arg
11
12
13

Google Sheet add increment numbers for duplicates

Hello how to get a increment numbers for duplicate with array function.
Example in below picture if we search a word in entire A row if there is any duplicates it will add numbers in B.
Try
=arrayformula(if(A1:A="",,
A1:A&if(countif(A1:A,A1:A)=1,,
"-"&text(countifs(A1:A,A1:A,row(A1:A),"<="&row(A1:A)),"00"))))
Or...
=A1&if(countif(A$1:A,A1)=1,,
"-"&text(countif(A$1:A1,A1),"00"))
...and dragdown.
Count duplicates
function countdups() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(1,1,sh.getLastRow(),2).getValues();
let uA = [];
let obj = {pA:[]};
vs.forEach(r => {
if(!~uA.indexOf(r[0])) {
uA.push(r[0]);
obj[r[0]] = 1;
obj.pA.push(r[0]);
} else {
obj[r[0]] += 1;
}
});
let vo = obj.pA.map(p => [`${p}-${obj[p]}`]);
sh.getRange(1,2,vo.length,1).setValues(vo);
}
COL1
COL1-1
7
7-2
6
6-1
9
9-1
11
11-3
16
16-1
4
4-1
15
15-1
19
19-3
11
2-1
2
10-3
10
18-1
11
5-1
10
0-1
18
19
19
10
5
0
7

Google sheets - Update date cells based on criteria

I need to update dates in a sheet once a week.
I would like to manually run a script to do this.
Each date in column "C" will increase by a number of days in column "D" in same row.
Only dates prior to a date in specific cell (F2 in example) will be updated.
See this sheet as an example: https://docs.google.com/spreadsheets/d/11f6G5_vNK5Z8UR2A_MUUXpL8awC2mJ8ozpnznWQ_anM/edit?usp=sharing
Column C - Service Date
Column D - No. of days to advance
12/3/2021
7
12/3/2021
14
12/10/2021
7
12/10/2021
7
12/17/2021
28
Any help or pointing in the right direction would be great!
function updateDates() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const vs = sh.getRange(2,1,sh.getLastRow() - 1, sh.getLastColumn()).getValues();
const dt = new Date(sh.getRange('F2').getValue());
const dthv = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
const oA = [];
vs.forEach(r => {
let d = new Date(r[2]);
let dv = new Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf();
if(dv < dthv) {
oA.push([new Date(d.getFullYear(),d.getMonth(),d.getDate() + r[3])]);
} else {
oA.push([r[2]]);
}
});
sh.getRange(2,3,oA.length,oA[0].length).setValues(oA);
}
Sheet 0 Before:
COL1
COL2
COL3
COL4
COL5
COL6
11/10/2021
5
11/15/2021
11/11/2021
6
11/12/2021
7
11/13/2021
8
11/14/2021
9
11/15/2021
10
11/16/2021
11
11/17/2021
12
11/18/2021
13
11/19/2021
14
11/20/2021
15
11/21/2021
16
11/22/2021
17
11/23/2021
18
11/24/2021
19
Sheet0 After:
COL1
COL2
COL3
COL4
COL5
COL6
11/15/2021
5
11/15/2021
11/17/2021
6
11/19/2021
7
11/21/2021
8
11/23/2021
9
11/15/2021
10
11/16/2021
11
11/17/2021
12
11/18/2021
13
11/19/2021
14
11/20/2021
15
11/21/2021
16
11/22/2021
17
11/23/2021
18
11/24/2021
19