简体   繁体   English

遍历文件夹中的多个电子表格,我想从每个表格中提取最后 3 列

[英]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:

在此处输入图像描述

References:参考:

  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.

相关问题 如何将谷歌表格中的信息提取到 forms 中的下拉菜单中,该下拉菜单将每分钟更新一次 - How do I pull over info from google sheets into a dropdown menu in forms that will update itself every minute 尝试制作宏以删除Google表格中的最后行数和列数 - Trying make a macro to delete last number of rows and columns in google sheets 我怎样才能阻止它覆盖数据并将其添加到谷歌表格的最后一行 - How can I stop this from over writing the data and adding it to last row in google sheets 问题:我想学习如何使用谷歌应用脚本将数字从网站导入谷歌表格 - Problem: I want to learn how to import a number from a website to google sheets using google apps script Google Script in Sheets, Reference 最后一行,找到票号和客户号,然后用它找到文件夹添加子文件夹 - Google Script in Sheets, Reference last row, to find Ticket number and customer number, then use it to find a folder to add a child folder 我想通过谷歌电子表格脚本从许多工作表中的一些工作表中更改单元格值 - I want change cell values from some sheet in many sheet by google spread sheet script 如何将数据从Patreon提取到Google表格中 - How to pull data from Patreon into Google Sheets 谷歌脚本将特定列从一张纸复制到另一张纸上的最后一行 - Google Script to copy specific columns from one sheet to another sheets last row onEdit 删除最后 X 个工作表,但按名称排除某些工作表 - Delete Last X Number of Sheets, But Exclude Certain Sheets by Name Google Sheets 在不断变化的工作表数量上对单元格求和 - Google Sheets Summing a cell over a changing number of sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM