[英]Iterating over a number of spread sheets in a folder and I want to pull last 3 columns from each
I am trying to find a solution to pull the data of a number of sheets into a single master doc.我正在尝试找到一种解决方案,将多张工作表的数据提取到单个主文档中。 I am only interested in the last three columns of each sheet, however the sheets do not all have the same number of columns.
我只对每张表的最后三列感兴趣,但是这些表的列数并不相同。
function getDataFromSpreadsheet(ssID) {
var ss = SpreadsheetApp.openById(ssID);
var ws = ss.getSheets()[0];
var data = ws.getRange("A1:W" + ws.getLastRow()).getValues();
return data;
}
I assume I can do something with getrow.length()[-3] but I am having no luck at all getting it to work.我想我可以用 getrow.length()[-3] 做一些事情,但我完全没有运气让它工作。
Change your range改变你的范围
from:从:
var data = ws.getRange("A1:W" + ws.getLastRow()).getValues();
to:至:
var data = ws.getRange(1, ws.getLastColumn()-2, ws.getLastRow(), 3).getValues();
Example:例子:
Sheet1:表 1:
Output: Output:
function get_last_three_columns_from_spreadsheet_and_sheet(
spreadsheet_id = SpreadsheetApp.getActiveSpreadsheet().getId(),
sheet_name = SpreadsheetApp.getActiveSheet().getName()
) {
const spreadsheet = SpreadsheetApp.openById(spreadsheet_id);
const sheet = spreadsheet.getSheetByName(sheet_name);
const last_column = sheet.getLastColumn();
const last_three_columns = [last_column, last_column - 1, last_column - 2];
const data = last_three_columns.map((column) => {
const last_row = sheet.getLastRow();
return sheet.getRange(1, column, last_row).getValues();
}, sheet);
Logger.log(data);
return data;
}
Posted on Reddit too, I'd try something like this.也发布在 Reddit 上,我会尝试这样的事情。 You can slap the function right into any spreadsheet script and run it.
您可以将 function 直接插入任何电子表格脚本并运行它。
By default, if you give it no arguments it'll use just the active spreadsheet and sheet.默认情况下,如果你不给它 arguments 它只会使用活动的电子表格和工作表。
If you call it from somewhere else, if you give it a spreadsheet and sheet name argument it'll get the last three columns of info if they're available.如果您从其他地方调用它,如果您给它一个电子表格和工作表名称参数,它将获得最后三列信息(如果它们可用)。 It'll error if you have a sheet with less than three columns.
如果您的工作表少于三列,则会出错。
The constant "last_three_columns" can be edited to get the last 4 or 5 or n by just adding another "last_column - n" to the array.只需将另一个“last_column - n”添加到数组中,就可以编辑常量“last_three_columns”以获得最后的 4 或 5 或 n。
If you want the function to work for some specified n of columns, it would need some adjustments but the original scope was for the last 3 of columns.如果您希望 function 用于某些指定的 n 列,则需要进行一些调整,但原始 scope 用于最后 3 列。
The result is an array of 2D Column arrays like,结果是二维列 arrays 的数组,例如,
// Output
[
[["Column 3"], ["data"], etc.],
[["Column 2"], ["data"], etc.],
[["Column 1"], ["data"], etc.]
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.