user15724106
user15724106

Reputation:

Showing only the last 12 rows in Google App Script

I am a beginner to appscript. I am doing a resident payment system where the user will enter username and password first and after logging in they can view the payment history. For now it is showing the all the payment data in the database but I want to only show the last 12 rows. Can anyone help me on this me? I have attached some images and link to my appscript to explain myself better. Thanks in advance.

https://script.google.com/d/1xV7FDVgp10XbGFtPJDUnhqm1GYybKZeKEI2L84Slp34rndOWgVJ1iScm/edit?usp=sharing - Link to my appscript coding

https://script.google.com/macros/s/AKfycbxFJTD9f6cMMDYDHw5sY6bENOmC52Z_7-mkGmnNGkQ-B5-j-63Q4aOcgetX4MWYIfW6/exec - Link to my executed version (Web App)

USERNAME : JJACKSON

PASSWORD : PASSWORD1

Image 1

Upvotes: 3

Views: 76

Answers (1)

Tanaike
Tanaike

Reputation: 201378

When I saw your script of your shared Google Apps Script project, in that case, how about the following modification? In this case, please modify the function of displayTable() at the HTML & Javascript side as follows.

From:

function displayTable(ar) {

To:

function displayTable(ar) {
  ar = ar.splice(-12); // <--- Added
  • In your question, the users cannot directly see your script. So, I explain a little about this modification. In your script, the function is displayTable is called after GetRecords at Google Apps Script side was called, when "Login" button is clicked. In this case, the values from GetRecords is the 2 dimensional array. In this answer, the value you expect is retrieved from this array by ar = ar.splice(-12).

Reference:

Added:

From your following replying,

Bro if I am not mistaken, it will only minus 12 rows right. In the AppScript right every year new 12 rows ( which means in 2022 new rows will be added from January 2022 until December 2022). So this logic will be a problem later bro. Hope you will me in this bro. Thank you bro.

In this case, how about the following modification?

From:

function displayTable(ar) {

To:

function displayTable(ar) {
  ar = ar.sort((a, b) => new Date(a).getTime() > new Date(b).getTime() ? -1 : 1).splice(-12); // <--- Added
  • In this case, the last 12 months are retrieved from ar.

  • If you want to retrieve the 12 months as the descending order, how about the following script?

      ar = ar.sort((a, b) => new Date(a).getTime() > new Date(b).getTime() ? 1 : -1).splice(0, 12);
    

Upvotes: 2

Related Questions