简体   繁体   English

如何使用 Apps 脚本从 Google 表格中的表格末尾获取 5 行?

[英]How do I get 5 rows from the end of my sheet in Google Sheets using Apps Script?

I have a sheet named 'Members' in my google sheets doc, from which I want to get the last 5 rows' data in JSON format using Google Apps Script.我的 google 表格文档中有一张名为“成员”的表格,我想使用 Google Apps 脚本从中获取 JSON 格式的最后 5 行数据。 I only have two columns in my sheet, so the JSON won't be big.我的工作表中只有两列,所以 JSON 不会很大。

I found out apps script has a getlastrow() function, which returns the last row of the sheet - will this function help me in my case?我发现应用程序脚本有一个 getlastrow() function,它返回工作表的最后一行 - 这个 function 对我有帮助吗? I am new to google apps script so I am unsure where to begin... How do I do this?我是谷歌应用程序脚本的新手,所以我不确定从哪里开始......我该怎么做? Please guide... Thanks!请指导...谢谢!

About I want to get the last 5 rows' data in JSON format using Google Apps Script.关于I want to get the last 5 rows' data in JSON format using Google Apps Script. , if your expected JSON format is like [{"header1": "value1", "header2": "value2"},,,] , how about the following sample script. ,如果您预期的 JSON 格式类似于[{"header1": "value1", "header2": "value2"},,,] ,那么下面的示例脚本怎么样。

Sample script:示例脚本:

Please copy and paste the following script to the script editor of Spreadsheet including "Members" sheet.请将以下脚本复制并粘贴到电子表格的脚本编辑器中,包括“成员”表。 And, please run this function.并且,请运行此 function。 By this, you can see the last 5 rows and the array including JSON data for each row in the log.这样,您可以看到日志中每一行的最后 5 行以及包含 JSON 数据的数组。

function myFunction() {
  const sheetName = "Members"; // Please set the sheet name.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  const [header, ...values] = sheet.getDataRange().getValues();

  const last5Rows = values.slice(-5); // This is last 5 rows.
  console.log(last5Rows); // You can see this value in the log.

  const obj = last5Rows.map(r => header.reduce((o, h, j) => (o[h] = r[j], o), {})); // This is an array including JSON data of each row.
  console.log(obj); // You can see this value in the log.
}
  • When this script is run, the values are retrieved from "Members" sheet.运行此脚本时,将从“成员”表中检索值。 And, the last 5 rows are retrieved, and an array including JSON data for each row is created.并且,检索最后 5 行,并创建包含每行的 JSON 数据的数组。

Note:笔记:

  • Unfortunately, I'm not sure whether I could correctly understand JSON format from your question.不幸的是,我不确定我是否能从您的问题中正确理解JSON format So, if the output result of above sample script was not your expected value, can you provide the sample input and output values?那么,如果上述示例脚本的 output 结果不是您的预期值,您能否提供示例输入和 output 值? By this, I would like to modify the script.通过这个,我想修改脚本。

References:参考:

暂无
暂无

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

相关问题 如何将我的应用程序脚本部署到我的谷歌表格中 - How do I deploy my apps script into my google sheets 使用 Apps 脚本从 Google 表格中的另一张表格中删除包含值的行 - Delete rows containing value from another sheet in Google Sheets using Apps Script 如何使用 Google Apps 脚本根据单元格值获取 google 工作表名称? - How do I get a google sheet name based on cell value using Google Apps Script? 如何从表格中计算谷歌应用脚​​本中的持续时间? - How do I calculate duration in google apps script from sheets? 在Apps脚本中使用相同工作表的Google表格API - Using Google Sheets API from within Apps Script for same Sheet 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? 如何使用 Google Apps 脚本将表格从 Google 表格上传到 AWS S3 作为 CSV? - How to upload a sheet from Google Sheets to AWS S3 as a CSV using google Apps Script? 如何使用 Google 表格中的 Java Apps 脚本将一行剪切并粘贴到另一张工作表? - How do you cut and paste a row to another sheet using Java Apps Script in Google Sheets? 如何使用 Google Apps 脚本 select 谷歌表格上的所有行? - How to select all rows on Google Sheets using Google Apps Script? 如何在 Google Sheets Scripts 中获取工作表的最后一行(不是数据的最后一行)? - How can I get the end row of my sheet (not the last row with data) in Google Sheets Scripts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM