简体   繁体   English

使用Google脚本重新排列工作表上的多列

[英]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 我正在尝试将VBA宏转换为将工作表上的列从列索引数组重新排列为Google脚本

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 我想返回所有列,但将数组中列出的列作为前3列,而所有其他列按相同顺序排列

The original vba is using header names but I am using column indices 原始的vba使用标头名称,但我使用列索引

I get error Incorrect range height, was 1 but should be 24 我收到错误Incorrect range height, was 1 but should be 24错误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 这是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. 数据获取2D数组,但后来脚本将其作为1D数组处理。 In other words, to reference a cell value, instead of data[index] use something like data[row][column] . 换句话说,要引用一个单元格值,请使用诸如data[row][column]类的代替data[index] 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. 单个等号=用于将对象/字面量分配给变量,因此此headers[F] = data[j]而不是进行比较,而将headers[F]的值替换为data[j]的对象,该对象最初是data[j]是一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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