Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I created a form and tried to search for records but with no success
[This is my sheet link][1]
[1]: https://docs.google.com/spreadsheets/d/1FyM3xaDE6LhdHSxwIBrw-NAx1Mcn_Dezzgi_JWJtDWU/edit?usp=sharing
var SEARCH_COL_IDX=0;
function Search(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName('フォーム');
//var data = ss.getSheetByName('データ管理');
var str= formS.getRange("M3").getValue();
var values= ss.getSheetByName("データ管理").getDataRange().getValues();
for(var i=0;i<values.length;i++){
var row= values[i];
if(row[SEARCH_COL_IDX] == str){
formS.getRange("D3").setValue(row[0]);//title
formS.getRange("D7").setValue(row[1]);//ID
formS.getRange("D9").setValue(row[2]);//daytime
formS.getRange("I9").setValue(row[3]);//Check data
formS.getRange("D13").setValue(row[4]);//input1
formS.getRange("D21").setValue(row[5]);//input2
formS.getRange("D30").setValue(row[6]);//input3
formS.getRange("D39").setValue(row[7]);//input4
formS.getRange("C50").setValue(row[8]);//Implementation date
formS.getRange("C55").setValue(row[9]);//Date of enactment
formS.getRange("J50").setValue(row[10]);//管理者
formS.getRange("J55").setValue(row[11]);//制定者
}
}
}
This is basically the same thing and it works:
function testfunc() {
var ss = SpreadsheetApp.getActive();
var sh2 = ss.getSheetByName('Sheet2');
var str = sh2.getRange('A1').getValue();//get search value for col1
var vs = ss.getSheetByName('Sheet1').getDataRange().getValues();
vs.forEach(r => {
if (r[0] == str) {
sh2.appendRow(r);//I appended the row because it's easier
}
});
}
My Sheet1:
COL1
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
6
12
4
21
6
19
23
20
2
18
12
4
15
1
25
15
7
14
15
2
18
16
22
10
19
19
3
20
29
12
24
28
7
13
12
11
27
26
11
4
0
28
6
0
14
2
23
25
23
28
5
23
7
16
6
29
9
9
12
22
23
15
5
7
7
14
17
9
10
0
4
1
0
25
16
2
19
24
20
12
19
8
3
2
20
22
28
14
8
15
16
6
23
24
18
17
20
2
13
12
24
5
23
29
19
17
23
10
22
12
8
11
25
6
15
2
7
14
10
17
21
29
4
25
9
27
0
15
29
19
4
28
16
15
4
19
16
7
6
15
19
17
0
5
23
11
26
18
16
2
14
2
7
6
16
21
18
26
29
1
17
8
29
1
1
15
11
12
24
20
22
19
6
13
1
6
6
29
5
26
17
7
24
1
18
7
21
20
26
29
16
28
9
12
21
16
7
5
8
9
5
7
27
23
12
9
19
18
20
26
22
15
23
4
27
29
16
15
22
17
25
7
19
15
17
18
24
25
7
2
7
18
14
1
10
23
14
1
29
3
11
5
22
9
0
26
4
23
23
4
10
12
16
19
28
6
25
0
0
14
24
9
18
9
29
19
8
8
11
22
20
29
23
15
0
27
14
19
2
22
18
18
2
22
24
7
26
20
11
23
15
7
17
15
12
6
15
24
8
21
My Sheet2:
22
Search Value
22
19
6
13
1
6
6
29
5
26
22
15
23
4
27
29
16
15
22
17
Related
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);
}
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
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
I have the following challenge so far I know how to use the Reduce Method to sum all values in one dim array.
var sum = array.reduce((accumulator, currentValue) => {
return accumulator + currentValue; });
But how can I use this Method on an 2d array? For example I want to sum all items from the 5. column in the 2d Array.
Not complete sure if this is want you're looking for
function sumfive() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const [h, ...v] = sh.getDataRange().getValues();
let sum = v.reduce((a, r) => {
let row = 0;
r.forEach((c, i) => {
if (i > 4) { row += Number(c); }
});
a.sum += row;
return a;
}, { sum: 0, getSum: function () { return this.sum; } }).getSum();
Logger.log(sum);
}
Data:
COL1
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
COL11
COL12
COL13
COL14
COL15
COL16
COL17
COL18
COL19
COL20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Execution log
11:37:54 AM Notice Execution started
11:37:55 AM Info 6750.0
11:37:56 AM Notice Execution completed
I'm trying to sort an array of "tile" objects in as3 by the value of its "realY" property.
This is my code:
tiles.sortOn("realY", Array.DESCENDING);
tiles.reverse();
for each(var t:Tile in tiles)
{
trace(t.nearness);
}
This is the output:
6
6
6
6
6
7
7
7
7
7
7
8
8
8
8
8
8
8
9
9
9
9
9
9
9
9
10
10
10
10
10
10
10
10
10
11
11
11
11
11
11
11
11
11
11
12
12
12
12
12
12
12
12
12
12
12
13
13
13
13
13
13
13
13
13
13
13
13
14
14
14
14
14
14
14
14
14
14
14
14
14
15
15
15
15
15
15
15
15
15
15
15
15
15
15
16
16
16
16
16
16
16
16
16
16
16
16
16
16
16
17
17
17
17
17
17
17
17
17
17
17
17
17
17
17
17
18
18
18
18
18
18
18
18
18
18
18
18
18
18
18
18
18
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
19
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
20
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
22
22
22
22
22
22
22
22
22
22
22
22
22
22
22
22
22
22
22
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
23
24
24
24
24
24
24
24
24
24
24
24
24
24
24
24
24
24
25
25
25
25
25
25
25
25
25
25
25
25
25
25
25
25
26
26
26
26
26
26
26
26
26
26
26
26
26
26
26
27
27
27
27
27
27
27
27
27
27
27
27
27
27
28
28
28
28
28
28
28
28
28
28
28
28
28
29
29
29
29
29
29
29
29
29
29
29
29
30
30
30
30
30
30
30
30
30
30
30
2
31
31
31
31
31
31
31
31
31
31
32
32
32
32
32
32
32
32
32
33
33
33
33
33
33
33
33
34
34
34
34
34
34
34
35
35
35
35
35
35
36
36
36
36
36
37
37
37
37
38
38
38
39
39
40
3
3
4
4
4
5
5
5
5
As you can see, there are some random small numbers at the end. Why is this happening?
Thanks
Try using your sortOn like this:
tiles.sortOn("realY", Array.NUMERIC | Array.DESCENDING);
By default Flash is sorting in alphabetical order. Check the documentation for more info.
Besides that, you're tracing the nearness property, not the realY property you sorted. So maybe that's a problem too.