简体   繁体   中英

Rearrange multiple columns on sheet with Google script

I am trying to convert a VBA macro that rearranges columns on a sheet from an array of column indices to a Google script

I want to return all columns but with the columns listed in the array as the first 3 columns and all the other columns in their same order

The original vba is using header names but I am using column indices

I get error Incorrect range height, was 1 but should be 24

I have no idea if there are other issues as well

Thanks in advance

Here is what I have

function myFunction() {
var sheet =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Elements");
var data = sheet.getDataRange().getValues();
var LC = sheet.getLastColumn();
var LR = sheet.getLastRow();

var headers = [4,2,3]; //array of colunm indices
var temp = [];

var Dex = 0
for (var i = 0; i < LC; i++) {
  for(var j = 0; j < LC; j++) { 
    for (var F = 0; F < headers.length - 1; F++) {
      if (headers[F] = data[j]) {
           Dex = F
           break;
       };
     }

      if (F < i) {
         temp = data.indexOf(i);
         data[i] = data.indexOf(j)
         data[j] = temp
        }
    };
   };

  sheet.getRange(1, 1, LC,LR).setValues([data])
}   

Here is the vba

Sub vba()
Dim rng As Range
Dim i As Integer
Dim J As Integer
Dim Temp
Dim nams As Variant
Dim F
Dim Dex As Integer

nams = Array("ItemID", "FirstName", "LastName", "Year")
Set rng = Range("A1").CurrentRegion

    For i = 1 To rng.Columns.Count
    For J = i To rng.Columns.Count
        For F = 0 To UBound(nams)
            If nams(F) = rng(J) Then Dex = F: Exit For
        Next F
        If F < i Then
            Temp = rng.Columns(i).Value
            rng(i).Resize(rng.Rows.Count) = rng.Columns(J).Value
            rng(J).Resize(rng.Rows.Count) = Temp
        End If
    Next J
Next i
End Sub

Try this:

function moveColumns() {
  var sh=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Elements");
  var rg= sh.getDataRange()
  var vA=rg.getValues();
  var vB=[];
  var first3 = [8,7,6,5];
  var first3desc=first3.slice();
  first3desc.sort(function(a, b){return b-a;});
  for(var i=0;i<vA.length;i++){
    var row=vA[i];
    for(var j=0;j<first3.length;j++){
      row.splice(j,0,vA[i][first3[j]-1+j]);
    }
    for(var j=0;j<first3desc.length;j++){
      var c=first3desc[j]-1+first3.length;//for debugging
      row.splice(first3desc[j]-1+first3.length,1);
    }
    vB.push(row);
  }
  sh.getRange(1,1,vB.length,vB[0].length).setValues(vB);
}   

Here's the function as I run it this time.

function moveColumns() {
  var sh=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Elements");
  var rg= sh.getDataRange()
  var vA=rg.getValues();
  var vB=[];
  var first3 = [8,7,6,5,4,3,2,1];
  var first3desc=first3.slice();
  first3desc.sort(function(a, b){return b-a;});
  for(var i=0;i<vA.length;i++){
    var row=vA[i];
    for(var j=0;j<first3.length;j++){
      row.splice(j,0,vA[i][first3[j]-1+j]);
    }
    for(var j=0;j<first3desc.length;j++){
      var c=first3desc[j]-1+first3.length;
      row.splice(first3desc[j]-1+first3.length,1);
    }
    vB.push(row);
  }
  sh.getRange(1,1,vB.length,vB[0].length).setValues(vB);
}   

My "Elements" sheet before:

在此处输入图片说明

My "Elements" sheet after:

在此处输入图片说明

On this line

var data = sheet.getDataRange().getValues();

data gets a 2D array, but later the script handled it as 1D array. In other words, to reference a cell value, instead of data[index] use something like data[row][column] .

A single equal sign = is use to assign an object/literal to a variable so this headers[F] = data[j] instead of doing a comparison replaces the value of headers[F] by the object of data[j] which initially is an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM