I have a function in javascript gotten from here
I tried to put it into the proper format for Google sheets with the sparse knowledge I have but have not gotten it to work
The function shifts columns in an array from indices in a list I am using chrome V8
The function gives the error Cannot read property length of undefined
for
sht2.getRange(1,1, result.length, result [0].length).setValues(result )
Thanks
This is what id does
Bob|Carol|Ted|Alice
a |b |c |d
1 |2 |3 |4
A |B |C |D
If the list of indices is list=[3] so I get
Alice|Bob|Carol|Ted
d |a |b |c
4 |1 |2 |3
D |A |B |C
list = [2,3]
Ted|Alice|Bob|Carol
c |d |a |b
3 |4 |1 |2
C |D |A |B
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sht = ss.getSheetByName('users');
var rng = sht.getDataRange();
var data = rng.getValues();
var tofront = [2,0,28,4]
var result = rearrange(data, [2,0,28,4])
var sht2 = ss.getSheetByName('AAA');
sht2.getRange(1,1, result.length, result [0].length).setValues(result );
}
function rearrange(rows, tofront) {
var rearrange = (rows=[], toFront=[]) => {
var originalHeaders = [...rows[0]]
var remainingHeaders = [...rows[0]]
var frontHeaders = toFront.map(index => {
// set this one to null, to be filtered out later
remainingHeaders[index] = null
// grab the original header value
return originalHeaders[index]
})
// you don't want to modify the original headers directly,
// seeing as you might not end up with the desired result
// if you pass a `toFront` value like [1,2] or where
// any of the values come after another
var newHeaders = [
...frontHeaders,
...remainingHeaders.filter(v => v !== null)
]
return data.map(r => {
let row = {}
r.forEach((value, i) => {
row[originalHeaders[i]] = value
})
return newHeaders.map(h => row[h])
})
}
}