简体   繁体   English

Google Apps 脚本从工作表中提取特定列并使用过滤器和 map 放入新工作表 — 慢

[英]Google Apps Script extract specific columns from sheet and put into new sheet using filter & map — slow

Thanks in advance for your help.在此先感谢您的帮助。 I'm new to apps script.我是应用程序脚本的新手。

I have a gsheet with 98 columns and 25000 rows.我有一个 98 列和 25000 行的 gsheet。 All I want to do is copy 24 of the 98 columns to a new sheet.我要做的就是将 98 列中的 24 列复制到新工作表中。

Currently I am using filter & map, and it works:目前我正在使用过滤器和 map,它可以工作:

var data = sourceSheet.getDataRange().getValues(); var data = sourceSheet.getDataRange().getValues(); //read all columns & rows into array //将所有列和行读入数组

var filterData = data.filter(function(e, j){return j > 0 && e}).map(function(e){return [e[88], e[14], e[13], e[4], e[17], e[87], e[91], e[48], e[57], e[31], e[89], e[82], e[70], e[97], e[47], e[30], e[72], e[71], e[67], e[34], e[33], e[00], e[38], e[39]]}); var filterData = data.filter(function(e, j){return j > 0 && e}).map(function(e){return [e[88], e[14], e[13], e[4 ], e[17], e[87], e[91], e[48], e[57], e[31], e[89], e[82], e[70], e[97 ], e[47], e[30], e[72], e[71], e[67], e[34], e[33], e[00], e[38], e[39 ]]}); //extract just the columns I want into a new array //将我想要的列提取到一个新数组中

but that 2nd line takes almost an hour to execute, I presume because it is processing every element 1-by-1.但是第二行需要将近一个小时才能执行,我想是因为它正在逐个处理每个元素。 even though it is just to return each one.即使它只是返回每一个。

I haven't tried getValue'ing and setValue'ing columns one at a time because everything I read says limit external calls and do everything in memory.我没有尝试一次一个 getValue'ing 和 setValue'ing 列,因为我读到的所有内容都说限制外部调用并在 memory 中执行所有操作。 And I can't imagine pushing elements 1-by-1 would be faster than filtering.而且我无法想象将元素一一推送会比过滤更快。

Suggestions for faster execution?更快执行的建议?

function doCopy(SpreadID, OrgSheet, DestSheet, OrgRange, Sql, DestCell) { 
  var mySpread = SpreadsheetApp.openById(SpreadID);
  var myQry = '=QUERY(' + OrgSheet + "!" + OrgRange + ',\"'+ Sql + '\")';
  var myDestSheet = mySpread.getSheetByName(DestSheet);
  myDestSheet.getRange(DestCell).setFormula(myQry);
}

Sample to call, but the destination sheet must be blank:要调用的示例,但目标表必须为空白:

doCopy(spreadsheetId,"Sheet1", "Sheet2", "A:G", "Select A, C, F", "A1");

Another way to look at the problem which might be more time-effective and easy to implement is the following:另一种看待问题的方法可能更省时且易于实施,如下所示:

  1. Duplicate the original sheet into a new one with copyTo(spreadsheet) ( https://developers.google.com/apps-script/reference/spreadsheet/sheet#copytospreadsheet ).使用copyTo(spreadsheet) ( https://developers.google.com/apps-script/reference/spreadsheet/sheet#copytospreadsheet ) 将原始工作表复制到新工作表中。 You might also want to check this post for more information about creating a sheet into the same spreadsheet ( Google Script to Duplicate Sheet that is Not Active )您可能还想查看这篇文章以获取有关在同一电子表格中创建工作表的更多信息( Google Script to Duplicate Sheet that is Not Active

  2. Delete the columns you don't want to keep with deleteColumn(columnPosition) or deleteColumns(columnPosition, howMany) (see https://developers.google.com/apps-script/reference/spreadsheet/sheet#deletecolumncolumnposition ).使用deleteColumn(columnPosition)deleteColumns(columnPosition, howMany)删除您不想保留的列(请参阅https://developers.google.com/apps-script/reference/spreadsheet/sheet#deletecolumncolumnposition )。 You might also want to start deleting columns from the right side of the sheet to make the implementation easier.您可能还想从工作表的右侧开始删除列,以使实施更容易。

Let me know if it helps.让我知道它是否有帮助。

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

相关问题 如何使用谷歌应用脚本在特定范围内从工作表中获取特定图表并将其粘贴到另一张工作表上? - How to get a specific chart from a sheet and paste it on another sheet, in a specific range using google apps script? Google 应用程序脚本 - 自动过滤工作表中的特定范围 - Google apps script - automatically filter a specific range within a sheet 谷歌应用脚本 select 特定列上传到目标表 - Google apps script select specific columns to upload into target sheet 如何使用 Apps 脚本在 google sheet 中提取 pubhtml ID? - How to extract the pubhtml ID in google sheet using Apps Script? 需要从 Google Apps 脚本查询 Google 表格以获取特定数据 - Need to Query Google Sheet for specific data from Google Apps Script Google Apps 脚本表将数字提取为字符串 - Google Apps Script Sheet extract numbers as string Google Apps 脚本 - 自动过滤活动工作表 - Google apps script - automatically filter the active sheet 使用Google Apps脚本将行复制到新工作表 - Copy row to new sheet using Google Apps Script 如何使用谷歌应用程序脚本在谷歌文档中的特定文本之后从工作表插入表? - How to insertTable from sheet after specific text in google docs using google apps script? Google Apps 脚本 - 格式化特定工作表中的列 - Google Apps Script - Format Column in Specific Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM