[英]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:另一种看待问题的方法可能更省时且易于实施,如下所示:
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 )
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.