简体   繁体   English

删除 Google App Script FAST 中的特定列

[英]Delete specific columns in Google App Script FAST

below is code the delete entire columns except columns "G" and "M" using a for loop, yet the process is the way too slow, is there a way to perform it faster?下面是使用for循环删除除“G”和“M”列之外的整个列的代码,但是这个过程太慢了,有没有办法更快地执行它?

  var lastCol = newSheet.getLastColumn();
  var keep = [7,13]; 

  for (var col=lastCol; col > 0; col--) {
  if (keep.indexOf(col) == -1) {
  newSheet.deleteColumn(col);
                }
     }

Here's how to do that with the SpreadsheetApp API:以下是使用 SpreadsheetApp API 执行此操作的方法:

function test() {
  const keep = [7, 13]; // columns G and M
  const newSheet = SpreadsheetApp.getActiveSheet();
  deleteColumns_(newSheet, keep);
}


/**
* Deletes all columns in sheet except the ones whose column numbers
* are listed in columnsToKeep.
*
* The columnsToKeep array [1, 2, 7, 13] means that columns A, B, G and M
* will remain while other columns are deleted.
*
* @param {Sheet} sheet The sheet where to delete columns.
* @param {Number[]} columnsToKeep Array of column numbers to keep.
*/
function deleteColumns_(sheet, columnsToKeep) {
  // version 1.0, written by --Hyde, 13 June 2022
  //   - see https://stackoverflow.com/q/72600890/13045193
  const columnsToDelete = [];
  for (let i = 1, maxColumns = sheet.getMaxColumns(); i <= maxColumns; i++) {
    if (!columnsToKeep.some(columnNumber => i === columnNumber)) {
      columnsToDelete.push(i);
    }
  }
  const tuples = getRunLengths_(columnsToDelete).reverse();
  tuples.forEach(([columnStart, numColumns]) => sheet.deleteColumns(columnStart, numColumns));
}


/**
* Counts consecutive numbers in an array and returns a 2D array that
* lists the first number of each run and the number of items in each run.
*
* The numbers array [1, 2, 3, 5, 8, 9, 11, 12, 13, 5, 4] will get
* the result [[1, 3], [5, 1], [8, 2], [11, 3], [5, 1], [4, 1]].
*
* For best results, sort the numbers array like this:
* const runLengths = getRunLengths_(numbers.sort((a, b) => a - b));
* Note that duplicate values in numbers will give duplicates in result.
*
* @param {Number[]} numbers The numbers to group into runs.
* @return {Number[][]} The numbers grouped into runs, or [] if the array is empty.
*/
function getRunLengths_(numbers) {
  // version 1.1, written by --Hyde, 31 May 2021
  if (!numbers.length) {
    return [];
  }
  return numbers.reduce((accumulator, value, index) => {
    if (!index || value !== 1 + numbers[index - 1]) {
      accumulator.push([value]);
    }
    const lastIndex = accumulator.length - 1;
    accumulator[lastIndex][1] = (accumulator[lastIndex][1] || 0) + 1;
    return accumulator;
  }, []);
}

I believe your goal is as follows.我相信你的目标如下。

  • You want to reduce the process cost of your script.您想降低脚本的处理成本。
  • You want to delete all columns except for columns "G" and "M".您想删除除“G”和“M”列之外的所有列。

In this case, how about using Sheets API?在这种情况下,使用 Sheets API 怎么样? When Sheets API is used, your script is as follows.使用 Sheets API 时,您的脚本如下所示。 I thought that when Sheets API is used, the process cost might be able to be reduced a little.我认为当使用 Sheets API 时,处理成本可能会降低一点。

Modified script:修改后的脚本:

Before you use this script, please enable Sheets API at Advanced Google services .在使用此脚本之前, 请在 Advanced Google services 中启用 Sheets API

function myFunction() {
  var sheetName = "Sheet1";  // Please set the sheet name.
  var keep = [7, 13];

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var newSheet = ss.getSheetByName(sheetName);
  var lastCol = newSheet.getLastColumn(); // or newSheet.getMaxColumns()
  var sheetId = newSheet.getSheetId();
  var requests = [...Array(lastCol)].reduce((ar, _, i) => {
    if (!keep.includes(i + 1)) {
      ar.push({ deleteDimension: { range: { sheetId, startIndex: i, endIndex: i + 1, dimension: "COLUMNS" } } });
    }
    return ar;
  }, []).reverse();
  if (requests.length == 0) return;
  Sheets.Spreadsheets.batchUpdate({ requests }, ss.getId());
}
  • When this script is run, all columns except for the columns "G" and "M" are deleted.运行此脚本时,除“G”和“M”列之外的所有列都将被删除。
  • From your script, var lastCol = newSheet.getLastColumn();从您的脚本中, var lastCol = newSheet.getLastColumn(); is used.用来。 In this case, the data range is used.在这种情况下,使用数据范围。 If you want to check all columns, please use var lastCol = newSheet.getMaxColumns();如果要检查所有列,请使用var lastCol = newSheet.getMaxColumns(); instead of it.而不是它。

References:参考:

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

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