How to combine multiple ranges in one variable? - google-apps-script

I have this globalVariables function with a variable array:
function globalVariables(){
var varArray = {
spreadsheetId : 'MY_SPREADSHEET_ID',
dataRange : 'Air Database!A2:P', // Where I want to include another range
idRange : 'Air Database!B2:B',
lastCol : 'S',
insertRange : 'Air Database!A1:P1',
sheetID : 'MY_SHEET_ID_#'
};
return varArray;
}
On dataRange above, I want to skip columns D & E. I want it to be something like:
dataRange : 'Air Database!A2:C,F2:P',
I've tried what I wrote above but it returned blank (even the A2:C didn't show up). I've also tried the ones I listed below and all of them returned the same thing (blank). aws is my sheet
dataRange : 'Air Database!A2:C' & 'Air Database!F2:P',
dataRange : 'Air Database!A2:C&F2:P',
dataRange : 'Air Database!A2:C && F2:P',
dataRange : 'Air Database!A2:C','Air Database!F2:P',
dataRange : ['Air Database!A2:C','Air Database!F2:P'],
dataRange : aws.getRangeList(['A2:C', 'F2:P']),
dataRange : [aws.getRange('A2:C'), aws.getRange('F2:P')],
UPDATE: No need to answer the question since I already just worked around the problem. If in case you do have tips for other people with the same problem, feel free to answer. Thank you!

My Sheet0:
COL1
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
7
8
4
15
14
14
9
16
17
2
10
17
0
11
10
5
6
17
10
16
8
4
12
0
16
17
2
16
15
0
14
13
17
12
5
2
13
9
11
14
0
4
15
4
9
3
14
8
16
1
6
3
19
17
14
8
19
0
0
1
18
9
2
6
7
16
13
13
8
0
12
9
9
15
17
12
1
1
18
14
8
4
18
1
15
18
2
16
17
3
19
3
12
0
18
2
4
3
13
7
6
5
3
5
6
17
11
13
6
19
1
9
3
2
9
2
12
8
10
14
19
14
6
18
2
17
13
2
4
18
11
16
2
14
2
17
17
13
5
9
10
10
12
1
6
3
6
4
12
11
3
10
16
14
13
17
2
9
15
13
10
7
0
15
13
3
13
19
17
10
18
8
14
9
9
8
12
18
12
8
15
17
7
17
4
3
18
1
7
6
11
3
3
12
0
19
4
7
1
0
Rangelist Code:
function myfunc() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const rgl = sh.getRangeList(['Sheet0!A2:C', 'E1:J']);
rgl.getRanges().forEach(r => {
let arr = r.getValues().filter(r => r[0]);
Logger.log('Range: ' + r.getA1Notation())
Logger.log(JSON.stringify(arr));
});
}
Execution log
11:27:09 AM Notice Execution started
11:27:10 AM Info Range: A2:C
11:27:10 AM Info [[7,8,4],[10,17,0],[8,4,12],[14,13,17],[6,3,19],[18,9,2],[12,9,9],[8,4,18],[19,3,12],[6,5,3],[1,9,3],[19,14,6],[11,16,2],[10,10,12],[3,10,16],[10,7,0],[18,8,14],[15,17,7],[11,3,3]]
11:27:11 AM Info Range: E1:J
11:27:11 AM Info [["COL5","COL6","COL7","COL8","COL9","COL10"],[14,14,9,16,17,2],[10,5,6,17,10,16],[16,17,2,16,15,0],[5,2,13,9,11,14],[9,3,14,8,16,1],[14,8,19,0,0,1],[7,16,13,13,8,0],[17,12,1,1,18,14],[15,18,2,16,17,3],[18,2,4,3,13,7],[6,17,11,13,6,19],[9,2,12,8,10,14],[2,17,13,2,4,18],[2,17,17,13,5,9],[6,3,6,4,12,11],[13,17,2,9,15,13],[13,3,13,19,17,10],[9,8,12,18,12,8],[4,3,18,1,7,6]]
11:27:11 AM Notice Execution completed

Related

How to make this sorting script run in all tabs except certain tabs (Sheets)?

I am trying to sort specific columns on a google sheet but exluding a few different tabs. I use google scripts a decent amount in my job but most of the scripts I get from Stack and then edit them because I am not a developer/engineer, I'm in a non-tech role but I know a slight amount about coding.
Can someone that knows coding tell me what I am doing wrong?
I know there is a more efficient way to do the naming conventions of the columns and the ascending and I will change that later but wanted to see if anyone knows why I am getting this error? Thanks!
I found this code from a Stack post that I changed that I feel like should work but I keep getting this error on my trigger which is "onEdit":
"Error: The coordinates of the range are outside the dimensions of the sheet."
function sortEverySheetTest() {
var excludeSheetNames = ["Sheet 1","Sheet 2","Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6", "Sheet 7", "Sheet 8"]; // <--- Added
var sortFirst = 5;
var sortFirstAsc = true;
var sortSecond = 6;
var sortSecondAsc = true;
var sortThird = 7;
var sortThirdAsc = true;
var headerRows = 2;
var activeSheet = SpreadsheetApp.getActiveSheet();
var sheetName = activeSheet.getSheetName();
if (excludeSheetNames.includes(sheetName)) return; // <--- Added
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
var range = sheet.getRange(headerRows+1, 1, sheet.getMaxRows()-headerRows, sheet.getLastColumn());
range.sort([{ column: sortFirst, ascending: sortFirstAsc }, { column: sortSecond, ascending: sortSecondAsc }, { column: sortThird, ascending: sortThirdAsc }]);
}
Try this:
function onMyEdit(e) {
e.source.toast("Entry");
const excl = ["Sheet 1","Sheet 2","Sheet 3", "Sheet 4", "Sheet 5", "Sheet 6", "Sheet 7", "Sheet 8"];
const sh = e.range.getSheet();
const shts = e.source.getSheets().filter(sh => !~excl.indexOf(sh.getName())).map(sh => sh.getName());
const idx = shts.indexOf(sh.getName());
if(~idx) {
e.source.toast("Gate1");
let rg = sh.getRange(3,1,sh.getLastRow() - 2,sh.getLastColumn());
rg.sort([{column:5,ascending:true},{column:6,ascending:true},{column:7,ascending:true}]);
}
}
Here's the markdown table for my sheet data after sorting:
A
B
C
D
E
F
G
H
I
J
2
COL1
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
3
10
17
6
7
0
6
14
14
6
0
4
2
4
33
19
0
16
0
0
19
14
5
3
18
16
5
2
7
9
5
16
19
6
16
0
12
3
3
4
13
19
17
12
7
14
2
19
4
6
14
2
16
12
8
8
10
5
5
13
10
3
8
2
15
15
9
14
5
16
9
10
13
0
17
16
7
10
4
9
8
4
10
13
10
17
19
0
11
0
9
8
7
11
0
15
4
14
19
12
17
12
2
9
12
4
13
5
11
17
13
14
4
0
5
14
5
1
5
17
2
14
5
5
18
19
14
9
0
5
9
2
15
15
5
13
15
16
2
7
1
14
19
16
10
15
18
17
16
13
16
13
10
11
17
9
12
1
3
17
3
5
1
2
9
18
12
5
16
10
17
5
1
8
14
9
19
16
2
99
2
17
8
11
2
2
14
20
19
13
16
13
17
16
11
10
14
15
21
2
18
9
10
18
3
16
1
17
19
22
2
7
12
6
18
9
3
6
13
18

Find if a value exists in a Google sheet on a certain column, in all the rows above the current row based on 2 criterias

I have the following scenario:
columns from A-Z and 100 rows
in each row for the Z column I want to find if the value in A column from the current row exists in the rows above in A column
then if exists, I would like to find if the B column for the matching rows have the cell completed with a value
for all the rows that are matching I would to receive the matching rows in an array list, not as rows or at least to be able to put a value like "mathing"/"not matching"
this should be an array formula
I've tried something like this, only for the first criteria, but somehow it checks only the current row.
=ARRAYFORMULA( IF(ROW(Z2:Z)>2, IF(MATCH(A2:A,$A$2:A&ROW(A2:A)-1),"matching","not matching"),"not matching"))
I check to see if it's the first row (as it has headers), and if it's the first row, then surely it can't have any data matching above
It will be great to have it as a google sheet formula but if it's not possible it could also be a google app script
Try this:
function myfunk() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const osh = ss.getSheetByName("Sheet1");
osh.clearContents();
const dsr = 2;
const vs = sh.getRange(dsr, 1, sh.getLastRow() - dsr + 1, sh.getLastColumn()).getDisplayValues();
let o = [];
vs.forEach((r, i) => {
if (i > 0) {
let as = vs.map(r => r[0]).slice(0, i);// suggested by DoubleUnary
let bs = vs.map(r => r[1]).slice(0, i);//suggested by DoubleUnary
let idx = as.indexOf(r[25]);
if (~idx && bs[idx]) {
o.push(['yes', dsr + i, dsr + idx, r[25], bs[idx]])
} else {
o.push(['no', dsr + i, ~idx ? as[idx] : '', r[25], ~idx ? bs[idx] : '']);
}
}
});
o.unshift(['Value', 'Test Row', 'Result Row', 'Z value', 'B value'])
Logger.log(JSON.stringify(o));
osh.getRange(1, 1, o.length, o[0].length).setValues(o);
}
My Data:
COL1
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
COL11
COL12
COL13
COL14
COL15
COL16
COL17
COL18
COL19
COL20
COL21
COL22
COL23
COL24
COL25
COL26
4
4
8
18
3
15
15
6
6
18
2
10
19
14
5
16
3
6
0
13
15
14
10
13
19
7
14
5
18
12
12
3
5
5
12
0
0
4
19
17
13
14
2
6
2
0
18
15
16
1
1
15
14
8
18
19
18
19
14
11
9
2
12
4
19
8
7
17
2
5
17
12
3
18
6
15
12
17
12
15
1
11
2
14
4
12
15
4
2
7
13
12
4
10
0
2
9
2
15
12
18
7
10
6
15
8
3
11
3
11
8
2
0
12
18
12
17
3
3
10
5
18
0
6
19
12
11
2
3
5
16
16
7
14
12
3
1
9
0
1
9
4
17
11
18
2
4
16
13
4
1
3
4
13
9
8
11
18
9
9
10
17
6
16
8
10
15
10
18
1
2
9
10
18
13
0
11
4
7
2
0
18
3
5
1
5
18
17
4
8
2
4
10
13
7
10
9
6
3
7
5
7
12
12
6
0
3
7
3
3
19
4
2
5
0
9
5
14
0
2
15
9
18
6
1
15
5
5
1
12
4
7
9
3
19
19
15
16
12
18
13
0
12
4
12
4
1
8
19
2
1
1
8
14
6
10
0
16
14
14
10
8
3
15
5
13
9
13
10
6
16
2
15
3
2
16
19
2
14
1
10
1
1
5
5
10
8
3
8
17
13
15
8
9
6
4
2
14
6
4
1
6
14
8
9
11
12
3
18
5
14
9
18
2
12
17
2
17
10
0
11
7
11
2
0
11
15
6
7
13
10
18
17
6
19
12
14
15
7
12
5
0
17
15
2
2
18
6
7
13
1
10
19
9
7
13
15
13
7
18
11
13
10
8
1
10
5
17
9
9
5
14
3
3
1
19
7
13
0
5
10
2
12
17
3
12
9
0
10
9
15
6
14
18
1
3
6
4
9
19
4
9
15
11
0
3
10
19
5
18
16
10
4
4
4
1
1
6
8
10
9
8
19
4
11
18
12
14
8
4
5
11
8
17
5
7
13
13
16
14
8
7
14
7
18
9
3
11
0
1
7
19
8
6
3
4
4
2
4
11
3
7
5
5
9
16
15
7
6
4
6
7
17
8
13
10
2
9
18
0
13
12
4
13
9
4
19
4
7
10
17
1
5
5
3
7
12
3
19
19
7
1
11
9
9
9
7
5
6
8
7
0
11
19
6
17
12
1
18
Results:
Value
Test Row
Result Row
Z value
B value
no
3
15
no
4
17
no
5
6
no
6
5
no
7
8
no
8
18
no
9
7
yes
10
9
3
5
yes
11
3
14
5
no
12
10
yes
13
3
14
5
yes
14
3
14
5
yes
15
12
10
8
yes
16
12
10
8
yes
17
2
4
4
no
18
8
8
yes
19
6
15
8
no
20
5
no
21
18

Sorting columns left to right in Google Sheets ascending order with app script

I'm trying to sort columns from left to right based on dates, here is an example of the issue that I'm facing:
https://docs.google.com/spreadsheets/d/1CuDW-VRZxrwXXjyBj4BeUleMFqL8DUQrW3sku6WjMh0/edit?usp=sharing
I'm sorting from column E to N based on the dates in row 6. The script that I'm currently using works okay as far as cell E6 has a date and there is no empty columns in between the full ones, otherwise the script won't work.
Here's the script that I'm using:
function sortLToR() {
//Defining the spreadsheet variables and setting ranges
var sheet = SpreadsheetApp.getActive().getSheetByName("Sort");
var range3 = sheet.getRange(5, 5, 88,sheet.getLastColumn()-4)
var range = sheet.getRange(5, 5, 88,sheet.getLastColumn()-4).getValues();
Logger.log(sheet.getLastColumn())
//Defining a blank array that can hold the result
var trans = [];
//transpose the data stored in range variable
for(var column = 0; column < range[0].length; column++){
trans[column] = [];
for(var row = 0; row < range.length; row++){
trans[column][row] = range[row][column];
}
}
function sortByDate(a, b) {
return new Date(b[1]).getTime() - new Date(a[1]).getTime();
}
var range2 = trans.sort(sortByDate);
var trans2 = [];
//transpose the data stored in range variable
for(var column = 0; column < range2[0].length; column++){
trans2[column] = [];
for(var row = 0; row < range2.length; row++){
trans2[column][row] = range2[row][column];
}
}
range3.setValues(trans2);
}
Any ideas how to fix this?
Thanks
Sort Columns by date and move blank columns to right so that data always starts in Column E
This version puts the blank columns to the right and always starts at E
function sortLToR() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const rg = sh.getRange(5, 5, sh.getLastRow() -4, sh.getLastColumn() - 4)
const vs = rg.getValues();
let b = transpose(vs);
b.sort((a,b) => {
if(a[1] && b[1]) {
return new Date(a[1]).valueOf() - new Date(b[1]).valueOf();
} else if(a[1] && !b[1]){
return -1;
} else if(!a[1] && b[1]) {
return +1
}
});
let c = transpose(b)
rg.setValues(c);
}
Demo:
Before Sort:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
5/19/2022
5/18/2022
5/17/2022
5/16/2022
5/15/2022
5/14/2022
5/13/2022
5/12/2022
5/11/2022
3
4
5
6
7
8
9
10
11
4
5
6
7
8
9
10
11
12
5
6
7
8
9
10
11
12
13
6
7
8
9
10
11
12
13
14
7
8
9
10
11
12
13
14
15
8
9
10
11
12
13
14
15
16
9
10
11
12
13
14
15
16
17
10
11
12
13
14
15
16
17
18
11
12
13
14
15
16
17
18
19
12
13
14
15
16
17
18
19
20
13
14
15
16
17
18
19
20
21
14
15
16
17
18
19
20
21
22
15
16
17
18
19
20
21
22
23
16
17
18
19
20
21
22
23
24
17
18
19
20
21
22
23
24
25
18
19
20
21
22
23
24
25
26
19
20
21
22
23
24
25
26
27
20
21
22
23
24
25
26
27
28
21
22
23
24
25
26
27
28
29
After Sort:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
COL10
COL9
COL8
COL7
COL6
COL5
COL4
COL3
COL2
5/11/2022
5/12/2022
5/13/2022
5/14/2022
5/15/2022
5/16/2022
5/17/2022
5/18/2022
5/19/2022
11
10
9
8
7
6
5
4
3
12
11
10
9
8
7
6
5
4
13
12
11
10
9
8
7
6
5
14
13
12
11
10
9
8
7
6
15
14
13
12
11
10
9
8
7
16
15
14
13
12
11
10
9
8
17
16
15
14
13
12
11
10
9
18
17
16
15
14
13
12
11
10
19
18
17
16
15
14
13
12
11
20
19
18
17
16
15
14
13
12
21
20
19
18
17
16
15
14
13
22
21
20
19
18
17
16
15
14
23
22
21
20
19
18
17
16
15
24
23
22
21
20
19
18
17
16
25
24
23
22
21
20
19
18
17
26
25
24
23
22
21
20
19
18
27
26
25
24
23
22
21
20
19
28
27
26
25
24
23
22
21
20
29
28
27
26
25
24
23
22
21
I'm not sure if this is a viable solution, but you could just add another sheet or use a range in columns to the right, I think you could accomplish what you want.
=transpose(sort(TRANSPOSE(filter(F:N,F:F<>"")),6,true))
See example here of before and after.
Updated to account for potential breaks in data:
=transpose(sort(TRANSPOSE(filter(Before!E:N,Row(Before!E:E)<=max(arrayformula(--NOt(ISBLANK(Before!E:N))*row(Before!E:N))))),6,true))
One could then leverage this formula in an appscript to overwrite the data with something like this:
function makeRange(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const yourSheetName = "Before"; //sheet name with data
const zFormula = "=transpose(sort(TRANSPOSE(filter('zzzzz'!E:N,Row('zzzzz'!E:E)<=max(arrayformula(--NOt(ISBLANK('zzzzz'!E:N))*row('zzzzz'!E:N))))),6,true))";//
var newSheet = ss.insertSheet();
var newFormula = zFormula.replace(/zzzzz/g,yourSheetName)
newSheet.getRange("E1").setFormula(newFormula);
var newValues = newSheet.getRange("E:N").getValues();
ss.getSheetByName(yourSheetName).getRange("E:N").setValues(newValues);
ss.deleteSheet(newSheet);
}

Google script to append multiple rows - select columns

How can I append multiple rows from source sheet to destination sheet; select columns like A,C,H ?
source sheet columns A-Z, with multiple rows.
destination sheet - append rows from source sheet - select columns A, C and H only.
Thank you
Transfer Data from one sheet to another selecting only columns A,C and H
function xferdata() {
const ss = SpreadsheetApp.getActive();
const ssh = ss.getSheetByName("Sheet0");
const dsh = ss.getSheetByName("Sheet1");
const vs = ssh.getRange(2,1,ssh.getLastRow() - 1, 8).getValues();
let o = vs.map(([a,,c,,,,,h]) => [a,c,h]);
//Logger.log(JSON.stringify(o));
dsh.getRange(dsh.getLastRow() + 1,1,o.length,o[0].length).setValues(o);
}
Sheet0:
COL1
COL2
COL3
COL4
COL5
COL6
COL7
COL8
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
9
3
4
5
6
7
8
9
10
4
5
6
7
8
9
10
11
5
6
7
8
9
10
11
12
6
7
8
9
10
11
12
13
7
8
9
10
11
12
13
14
8
9
10
11
12
13
14
15
9
10
11
12
13
14
15
16
10
11
12
13
14
15
16
17
11
12
13
14
15
16
17
18
12
13
14
15
16
17
18
19
13
14
15
16
17
18
19
20
14
15
16
17
18
19
20
21
15
16
17
18
19
20
21
22
16
17
18
19
20
21
22
23
17
18
19
20
21
22
23
24
18
19
20
21
22
23
24
25
19
20
21
22
23
24
25
26
20
21
22
23
24
25
26
27
Sheet1:
A
B
C
1
3
8
2
4
9
3
5
10
4
6
11
5
7
12
6
8
13
7
9
14
8
10
15
9
11
16
10
12
17
11
13
18
12
14
19
13
15
20
14
16
21
15
17
22
16
18
23
17
19
24
18
20
25
19
21
26
20
22
27

Timed out script

I'm using the following code in order to avoid using an IMPORTRANGE() formula. The data I'm getting is over 50k rows so that's why I'm using App Script.
But now I'm getting the following error:
Exception: Service Spreadsheets timed out while accessing document with id
function Live_Data_importing() {
var raw_live = SpreadsheetApp.openByUrl("someURL").getSheetByName("Sheet1");
var selected_columns = raw_live.getRange("A:Q").getValues().map(([a,,,,e,f,g,,i,j,,l,,n,o,]) => [a,e,f,g,i,j,l,n,o] );
var FF_filtered = selected_columns.filter(row=>row[8]=="somedata");
var FF_Hourly_live_data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("somesheet");
FF_Hourly_live_data.getRange(2, 1, FF_filtered.length, FF_filtered[0].length).setValues(FF_filtered);
}
How can I fix this error? I also made a copy of the file but script is still running the same error.
Tested this on smaller set of data
It works okay
function Live_Data_importing() {
const ss = SpreadsheetApp.getActive();
var raw_live = ss.getSheetByName("Sheet3");
var selected_columns = raw_live.getRange("A1:Q" + raw_live.getLastRow()).getValues().map(([a, , , , e, f, g, , i, j, , l, , n, o,]) => [a, e, f, g, i, j, l, n, o]);//fix this range
var FF_filtered = selected_columns.filter(row => row[8] == 0);//My data is all integers
var FF_Hourly_live_data = ss.getSheetByName("Sheet4");//output sheet
FF_Hourly_live_data.getRange(2, 1, FF_filtered.length, FF_filtered[0].length).setValues(FF_filtered);
}
Data:
COL1
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
COL11
COL12
COL13
COL14
COL15
COL16
COL17
COL18
COL19
COL20
0
1
18
13
0
7
5
11
15
2
1
17
0
8
11
18
0
16
1
8
0
16
16
9
5
0
16
8
14
0
7
4
3
15
8
19
18
4
18
10
9
14
7
16
17
13
17
4
8
6
19
18
9
18
12
1
8
19
12
10
13
17
4
2
10
3
7
16
14
13
1
5
16
12
2
3
14
3
0
9
4
17
14
5
9
13
18
0
4
1
14
1
12
7
10
1
15
4
14
5
1
17
13
17
3
1
17
8
19
7
14
18
2
17
1
16
2
19
13
15
7
4
9
0
11
14
11
7
12
14
3
19
14
13
18
12
16
19
19
6
7
6
10
5
15
8
0
8
2
16
4
14
18
14
2
16
16
5
15
16
4
5
9
6
6
2
1
15
14
8
19
8
4
10
12
12
4
6
10
12
11
15
3
4
1
3
17
19
10
4
11
2
10
16
12
1
6
3
0
3
11
0
14
8
13
13
4
2
16
18
10
14
5
3
7
4
7
9
5
15
0
6
5
1
2
18
1
1
11
13
7
13
5
15
5
13
17
18
14
19
0
17
10
6
10
16
16
0
18
19
12
8
15
1
11
4
19
4
17
14
4
14
14
6
16
8
15
4
5
2
4
5
14
14
16
9
0
16
0
4
8
3
8
5
12
15
5
19
14
0
1
6
12
2
19
10
10
19
13
19
0
5
14
7
2
17
3
10
2
14
2
5
0
18
5
1
13
13
3
13
10
18
18
4
18
11
0
7
13
9
18
13
9
2
0
16
15
3
9
14
12
15
15
7
16
13
14
1
4
12
18
9
6
14
18
11
16
2
18
15
15
0
2
4
6
16
5
18
2
6
14
18
11
1
9
15
13
8
8
19
11
19
14
0
7
15
1
10
8
9
14
14
15
3
11
13
4
10
5
16