Is there a way to increase and decrease 2 values by 1, and saving those values to be used the next time that function run

I’m a beginner programmer looking to make a Twitter bot for 2 reasons. 1. to learn and get comfortable with Java and 2. Splatoon 3 just got announced and I want to make a funny gimmick account ūüôā I know the basics of Java, I have a Twitter API and app functional, and a make shift environment made from a google sheets tweet scheduler.

The bot sends out a tweet everyday counting down the days until 2022 (the first possible day for the game to release) and counting up the days since the games first trailer in the latest Nintendo Direct

I need help finding a way to decrease and increase the values respectively and saving those values to be used the next time the function is run

The code:

function tweetS3DaysLeft()

 let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 let sheet = spreadsheet.getSheetByName("tweetS3DaysLeft");
 console.log("Sending tweet...");

//Starting values for the dates

 let DaysUntil = 316
 let DaysFrom = 1

 //Increase and decrease dates

 DaysUntil = DaysUntil - 1
 DaysFrom = DaysFrom +1
 // Build the tweet

 let tweetText = "Splatoon 3 is at least " + DaysUntil + " days until launch, and has been " + DaysFrom + " days since the first trailer";

 // Tweet


Any tips or links are appreciated. Thank you

22 thoughts on “Is there a way to increase and decrease 2 values by 1, and saving those values to be used the next time that function run”

  1. The best way would be to store them as global variables (i.e. outside of the function). I’m assuming that the file would be running at all times and not closed. If the file would be closed or just for safety, you can use node.js or something else to write the data to a local file.

  2. If I’m understanding it right, you want a way to locally save the DaysUntil and DaysFrom values.

    you can chuck em in the browser/local storage.

    function tweetS3DaysLeft()
     let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
     let sheet = spreadsheet.getSheetByName("tweetS3DaysLeft");
     console.log("Sending tweet...");
    //Starting values for the dates
     let DaysUntil = 316
     let DaysFrom = 1
     //Increase and decrease dates
     let saved_DaysUntil = localStorage.getItem("DaysUntil");
     if(!saved_DaysUntil){saved_DaysUntil = DaysUntil;}
     let saved_DaysFrom  = localStorage.getItem("DaysFrom");
     if(!saved_DaysFrom){saved_DaysFrom = DaysFrom;}
     DaysUntil = saved_DaysUntil - 1
     DaysFrom = saved_DaysFrom + 1
     localStorage.setItem("DaysFrom ",DaysFrom )
     // Build the tweet
     let tweetText = "Splatoon 3 is at least " + DaysUntil + " days until launch, and has been " + DaysFrom + " days since the first trailer";
     // Tweet

Leave a Comment