Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何显示google sheet中最新的3个数据_Arrays_Post_Google Apps Script_Google Sheets_Web Applications - Fatal编程技术网

Arrays 如何显示google sheet中最新的3个数据

Arrays 如何显示google sheet中最新的3个数据,arrays,post,google-apps-script,google-sheets,web-applications,Arrays,Post,Google Apps Script,Google Sheets,Web Applications,我正在做一个电报机器人来取回我所有的费用。我正在做一个功能检索最新的3个费用 这是列出我的费用的部分,它将显示所有费用 function doPost(e) { var contents = JSON.parse(e.postData.contents); var ssId = "<< ssId >>"; var sheet = SpreadsheetApp.openById(ssId).getSheetByName("Sheet1&qu

我正在做一个电报机器人来取回我所有的费用。我正在做一个功能检索最新的3个费用

这是列出我的费用的部分,它将显示所有费用

function doPost(e) {
var contents = JSON.parse(e.postData.contents); 
var ssId = "<< ssId >>";
var sheet =  SpreadsheetApp.openById(ssId).getSheetByName("Sheet1");
if (contents.callback_query) {
    var id = contents.callback_query.from.id;
    var data = contents.callback_query.data;
    if (data == 'expenses') {
      var expenses = [];
      var lr = sheet.getDataRange().getLastRow();
      
      for(var i = 6; i <=lr; i++) { //my expenses record start from cell A6
        var date = sheet.getRange(i,1).getValue();
        
        var month = new Array();
        month[0] = "January";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June";
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December"; 
        
        var formatedDate = date.getDate() + " " + (month[date.getMonth()]) + " " + date.getFullYear();     
        var item = sheet.getRange(i,2).getValue();
        var price = sheet.getRange(i,3).getValue();
        
        expenses.push("\n" + formatedDate + " | " + item + " | " + price );
        var expenseList = expenses.join("\n");
      }
      sendText(id, decodeURI( "Here your last expenses: " + "\n" + expenseList ));
    }

既然您已经将每个费用推送到数组
费用
,为什么不使用该方法,用您选择的
费用的一部分创建一个新数组

例如,最后一行可以更改为:

sendText(id,decodeURI(“这里是您最后的费用:”+“\n”+费用.slice(-3).join(“\n”))


您可以不使用
expenseList
变量。

您的问题目前很糟糕,代码不完整,缺少标记,并且格式不正确。由于您是新用户,欢迎阅读
expenseList
是字符串而不是数组。
My google sheet looks like
Date        | Expenses | Price
03 Mar 2020 | Cookies  | 6
04 Mar 2020 | Bread    | 5
05 Mar 2020 | Drinks   | 7
06 Mar 2020 | Chocolate| 8
07 Mar 2020 | Buns     | 7
08 Mar 2020 | Sweets   | 7