I want to remove rows from an array by row Index
I have
function test() {
arr1 = [[Id, From, To], [1.0, AA1, BB1], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
arr1.splice(1,1);
return arr1
}
I get
[[Id, From, To], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
Whitch is what I want
But If I have instead
function test() {
var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
arr1 = [[Id, From, To], [1.0, AA1, BB1], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
var result = arr1.splice(1,1);
sht.getRange(1,1,result.length,result[0].length).setValues(result)
}
I get
result = [[1.0, AA1, BB1]]
I do not get what is going on, how do I get
result = [[Id, From, To], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
I want to remove rows from an array by row Index
I have
function test() {
arr1 = [[Id, From, To], [1.0, AA1, BB1], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
arr1.splice(1,1);
return arr1
}
I get
[[Id, From, To], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
Whitch is what I want
But If I have instead
function test() {
var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
arr1 = [[Id, From, To], [1.0, AA1, BB1], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
var result = arr1.splice(1,1);
sht.getRange(1,1,result.length,result[0].length).setValues(result)
}
I get
result = [[1.0, AA1, BB1]]
I do not get what is going on, how do I get
result = [[Id, From, To], [2.0, AA2, BB2], [3.0, AA3, BB3], [4.0, AA4, BB4], [5.0, AA5, BB5], [6.0, AA6, BB6]]
Share
Improve this question
edited Jun 3, 2020 at 13:58
0Valt
10.4k9 gold badges39 silver badges64 bronze badges
asked Jul 14, 2019 at 13:10
xyzxyz
2,30011 gold badges49 silver badges71 bronze badges
4
-
why not simply use method
deleteRow
method ? – Code Maniac Commented Jul 14, 2019 at 13:22 - 3 Return arr1 rather than the result array – James Commented Jul 14, 2019 at 13:33
- @CodeManiac It'll be slow pared to whatever can be done inside js itself. – TheMaster Commented Jul 14, 2019 at 15:43
-
@op Quote your strings:
[["Id", "From","To"], [1.0,..
or provide number arrays. Your example isn't reproducible otherwise. – TheMaster Commented Jul 14, 2019 at 15:46
1 Answer
Reset to default 5Please, carefully read the documentation on splice()
method. When invoked on an Array
, it takes three possible arguments:
start
- index ofArray
element to start from;deleteCount
- number of elements to delete starting fromstart
;item
- ma-separated list of values to replace deleted values by.
Scenario 1
As you invoke it with two arguments splice(1,1)
, the method starts from index 1 ([1.0, AA1, BB1]
), deletes only this element and replaces it with nothing, mutating the original Array
as expected.
Scenario 2
The method also returns all elements you deleted as a separate Array
, if you want to return the original Array
(mutated by splice()
), you will need to remove the var result
and return the arr1
directly.
Useful links
splice()
method reference;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744338830a4569278.html
评论列表(0条)