[英]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"},,,]
,那么下面的示例脚本怎么样。
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.
}
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?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.