简体   繁体   English

在 Google 电子表格中移动一列

[英]Moving a column in Google Spreadsheet

Now I feel like I'm asking too many questions on this site :), but it seems like no has asked this question.现在我觉得我在这个网站上问了太多问题:),但似乎没有人问过这个问题。 IF they did, please link me there.如果他们这样做了,请把我链接到那里。

I just want to move a particular column to another place (before or after another column) in the same sheet.我只想将特定列移动到同一工作表中的另一个位置(在另一列之前或之后)。 I see options to hide, insert, delete, etc. But something as fundamental as moving, I don't see.我看到了隐藏、插入、删除等选项。但是像移动这样基本的东西,我没有看到。

Do I really have to get the range of the column data and then copy it, delete the column, insert a column where I want, get the range, then paste the data..?我真的必须获取列数据的范围,然后复制它,删除该列,在我想要的位置插入一列,获取范围,然后粘贴数据..?

Example:例子:

Col A  B  C  D  E  F  G  H
    1  2  3  4  5  6  7  8
    9  10 11 12 13 14 15 16

Move column B to before G say:将 B 列移到 G 之前说:

Col A  B  C  D  E  F  G  H
    1  3  4  5  6  2  7  8
    9  11 12 13 14 10 15 16

Here's an approach that will be much faster than using the SpreadsheetApp methods. 这是一种比使用SpreadsheetApp方法快得多的方法。 Instead of the copy / delete / insert / paste operations, you can take a copy of the whole sheet at once, modify in memory, then write the modified data back out. 您可以立即获取整张表的副本,在内存中进行修改,然后将修改后的数据写回来,而不是复制/删除/插入/粘贴操作。

We start with a moveColumn() function that has the same signature as in ssurendr's answer. 我们从一个moveColumn()函数开始,该函数具有与ssurendr的答案相同的签名。 However, its job is just to read the spreadsheet data, pass it to be manipulated by a generic javascript function, then write the result back out to the sheet. 但是,它的工作只是读取电子表格数据,将其传递给一般的javascript函数进行操作,然后将结果写回工作表。

/**
 * Move column in current spreadsheet
 *
 * @param {int}   iniCol  Source column index (1-based)
 * @param {int}   finCol    Destination column index (1-based)
 */
function moveColumn(iniCol, finCol) {
  var dataRange = SpreadsheetApp.getActiveSheet().getDataRange();
  var data = arrayMoveColumn( dataRange.getValues(), iniCol - 1, finCol - 1 );
  dataRange.setValues(data);
}

The next function, arrayMoveColumn() , is not specific to Google spreadsheets. 下一个函数arrayMoveColumn()并非特定于Google电子表格。 It will move a column in any two-dimensional array. 它将在任何二维数组中移动一列。 Javascript arrays are indexed starting at 0 , while Spreadsheet methods use 1-based indexes. Javascript数组从0开始编制索引,而Spreadsheet方法使用从1-based索引。 We've added some basic error checking, although it's not fool-proof. 我们添加了一些基本的错误检查,虽然它不是万无一失的。

The workhorse in this function is the Array.splice() method, which is used to remove and insert elements in the array. 此函数中的主力是Array.splice()方法,该方法用于删除和插入数组中的元素。

/**
 * Move content of a "column" in a 2-d Array.
 *
 * @param {array}  data  Two-dimensional matrix (array with no null values)
 *                       Array content is changed by this function.
 * @param {int}    from  Source column index (0-based)
 * @param {int}    to    Destination column index (0-based)
 *
 * @return {array}       Resulting array (for chaining)
 */
function arrayMoveColumn( data, from, to ) {
  // Parameter error checking
  if ( !( data instanceof Array && data[0] instanceof Array ) ) throw new TypeError( 'need 2d array' );
  if ( from >= data[0].length || to >= data[0].length ) throw new Error( 'index out of bounds' );

  for (var row=0; row<data.length; row++) {
    var temp = data[row].splice(from, 1);  // take 'from'
    data[row].splice(to, 0, temp[0]);      // put  'to'
  }
  return data;
}

I guess there is no other way except doing it like I said earlier. 我想除了我之前说的那样,除此之外别无他法。 Here is a function I made, if some people who come asking for the same thing need it. 这是我做的一个功能,如果有些人要求同样的东西需要它。 You can easily modify this to move rows as well. 您可以轻松修改它以移动行。 I'm new to Apps Script, so there may be an easier code than this. 我是Apps Script的新手,因此可能会有比这更简单的代码。

function moveColumn(iniCol, finCol) {
  // iniCol - Column of interest. (Integer)
  // finCol - Column where you move your initial column in front of.(Integer)
  // Ex:
  // Col A  B  C  D  E
  //     1  2  3  4  5
  //     6  7  8  9  10
  //     11    12 13 14
  // Want to move Column B in between Column D/E.
  // moveColumn(2,4);
  // Col A  B  C  D  E
  //     1  3  4  2  5
  //     6  8  9  7  10
  //     11 12 13    14
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getActiveSheet();
  var lRow = sh.getMaxRows();
  if (finCol > iniCol) {
    sh.insertColumnAfter(finCol);
    var iniRange = sh.getRange(1, iniCol, lRow);
    var finRange = sh.getRange(1, finCol + 1, lRow);
    iniRange.copyTo(finRange, {contentsOnly:true});
    sh.deleteColumn(iniCol);
  }
  else {
    sh.insertColumnAfter(finCol);
    var iniRange = sh.getRange(1, iniCol + 1, lRow);
    var finRange = sh.getRange(1, finCol + 1, lRow);
    iniRange.copyTo(finRange, {contentsOnly:true});
    sh.deleteColumn(iniCol + 1);    
  }
}

I figured that ideally anything we can do in google spreadsheet in the frontend could be done programmaticly. 我认为理想情况下,我们在前端谷歌电子表格中可以做的任何事情都可以通过编程方式完成。 (Like moving a column or row.) (比如移动一列或一行。)

If someone wants to replace two columns: (thanks @ssurendr for the basic) 如果有人想要替换两列:(感谢@ssurendr为基础)

function replaceColumn(Col1, Col2) {
// Ex:
// Col A  B  C  D  E
//     1  2  3  4  5
//     6  7  8  9  10
//     11    12 13 14
// Want to replace Column B & D.
// moveColumn(2,4);
// Col A  D  C  B  E
//     1  4  3  2  5
//     6  9  8  7  10
//     11 13 12    14
if (Col2 > Col1) {
  sh.insertColumnAfter(Col1);
  var iniAftRa = sh.getRange(1, Col2+1, lRow); 
  var finRange = sh.getRange(1, Col1, lRow);
  var finColAftRange = sh.getRange(1, Col1+1, lRow);
  finRange.copyTo(finColAftRange, {contentsOnly:true});
  iniAftRa.copyTo(finRange, {contentrsOnly:true});
  finColAftRange.copyTo(iniAftRa, {contentrsOnly:true});
  sh.deleteColumn(Col1 + 1);  
} else {
  sh.insertColumnAfter(Col2);
  var iniAftRa = sh.getRange(1, Col1+1, lRow); 
  var finRange = sh.getRange(1, Col2, lRow);
  var finColAftRange = sh.getRange(1, Col2+1, lRow);
  finRange.copyTo(finColAftRange, {contentsOnly:true});
  iniAftRa.copyTo(finRange, {contentrsOnly:true});
  finColAftRange.copyTo(iniAftRa, {contentrsOnly:true});
  sh.deleteColumn(Col2 + 1);    
}
}

News form the future!!新闻形成未来!! You just need to use moveColumns(columnSpec, destinationIndex)你只需要使用moveColumns(columnSpec, destinationIndex)

https://developers.google.com/apps-script/reference/spreadsheet/sheet#movecolumnscolumnspec,-destinationindex https://developers.google.com/apps-script/reference/spreadsheet/sheet#movecolumnscolumnspec,-destinationindex

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

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