Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

arrays - Javascript/GAS- How can I loop through dates in a POST request

I would like to post some timesheet entries for the next month into a system via API. For this I need

  1. user_id
  2. assignable_id
  3. Dates (Format YYYY-MMM-DD)
  4. Hours

First I downloaded user_id and assignable_id and stored them in an array and now am trying to use them to send a POST request to the system.

My challenge is, I am having trouble getting dates into an array and loop through each day in the array whenever the POST requests are made. Any pointers on how I can achieve this inside the POST loop?

Here is the code for POST request:

function demo_Code()
{
var user_dt = user_assignments()

for (var i = 0; i < user_dt.length; i++)
  {

     var data = {

      'hours': 0,
//      'date': lastRow[0][2],   // This is where I need to change and have next one month datesin an array instead of reading through the sheet
      'user_id': user_dt[i].user_id,
      'assignable_id': user_dt[i].assignable_id,
    };
    var payload = JSON.stringify(data);
    var options = {
      'method': 'POST',
      'Content-Type': 'application/json',
      'payload': data,
    };

    var url = 'https://api.10000ft.com/api/v1/users/' + data.user_id + '/time_entries?auth=' + token
    var response = UrlFetchApp.fetch(url, options);
     }

}
question from:https://stackoverflow.com/questions/66052441/javascript-gas-how-can-i-loop-through-dates-in-a-post-request

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From what you have described in your question and comments your intention is to generate a date array containing all the days between two days.

To achieve so, you could use the simple code below that has self explanatory comments. It basically iterates over all the days between two dates by keeping a "date count" that increases the day by one after each day is added to your array. This is achieved using the functions getDate() and setDate().

If you want to use this array then in your POST for loop you can simply access each date element by its index dateArray[2] for example and this index can also be your iteration variable i (dateArray[i]).

 // Array of dates
 var dateArray = [];
 // Starting date
 var date1= new Date(2021,1,1);
 // Ending date for the range of dates
 var date2 = new Date(2021,1,31);

// Set the current date to the starting date 
 var date = new Date(date1); 

// Iterate over all the dates from the starting
// to the end date
 while(date<=date2){
 // push the current date
   dateArray.push(date);
 // move the current date to be one day later
   date.setDate(date.getDate()+1);
 }
// log your date array
 Logger.log(dateArray);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...