Get scheduled transactions for current month alone

Can someone please update this function to grab the current month scheduled transactions?  I tried but I think I am lost. I should admit that I am 0 in coding. Thanks in advance!

function get_ynab_scheduled_transactions(accessToken, budgetId) {
  const transactions = fetch_ynab_data(accessToken, "budgets/" + budgetId + "/scheduled_transactions").scheduled_transactions;
  const headers = ["Account Name", "Date", "Payee", "Category", "Amount"];
  const rows = transactions.map(function(t){
   const amount = t.amount / 1000;
    return [t.account_name, t.date_first, t.payee_name, t.category_name, amount]
  });
  return [headers].concat(rows);
}
7replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Gray Horse When you run that, what do you get? Results? No results? An error? If you provide some detail I think we can get you on track. 馃檪

    Reply Like
      • Gray Horse
      • Gray_Horse.5
      • 5 mths ago
      • Reported - view

      George Thanks for your response, George. I do get the results. Now I have changed the code to t.date_next which gives me whole of scheduled transaction ranging from this month to future months. I want to grab results only for current running month. Hope that explains your question. Thanks again!

      Reply Like
  • Gray Horse This code is untested, but it should point you in the right direction. I've tried to use variable names and comments to explain what's going on, but let us know if you have any questions.

    function get_ynab_scheduled_transactions(accessToken, budgetId) {
      const allTransactions = fetch_ynab_data(accessToken, "budgets/" + budgetId + "/scheduled_transactions").scheduled_transactions;
    
      const today = new Date();
      const firstDayOfNextMonth = new Date(today.getFullYear(), today.getMonth() + 1);
    
      // Filter out all scheduled transactions that will next fire on or after the
      // first day of next month.
      const currentMonthTransactions = allTransactions.filter(txn => txn.date_next < firstDayOfNextMonth);
    
      const rows = currentMonthTransactions.map(function(t){
       const amount = t.amount / 1000;
        return [t.account_name, t.date_next, t.payee_name, t.category_name, amount]
      });
    
      const headers = ["Account Name", "Date", "Payee", "Category", "Amount"];
      return [headers].concat(rows);
    }
    
    Reply Like
      • Gray Horse
      • Gray_Horse.5
      • 5 mths ago
      • Reported - view

      Thanks, George Throws not defined error on this line

      const currentMonthTransactions = allTransactions.filter(txn >= txn.date_next < firstDayOfNextMonth);

      Cannot figure out where that "txn" was declared.

      Reply Like
      • George
      • Developer
      • george_ynab
      • 5 mths ago
      • Reported - view

      Gray Horse The `filter()` call will iterate through every transaction in `allTransactions`, assigning the current transaction going though the loop to the `txn` variable.

      The docs for `filter()` are pretty helpful. It might be worthwhile to read up on functions, too.

      Let us know if you have more questions. Good luck!

      Reply Like
      • Gray Horse
      • Gray_Horse.5
      • 5 mths ago
      • Reported - view

      Sorry! George It doesn't work in Google Script. Throws error!

      Reply Like
      • George
      • Developer
      • george_ynab
      • 5 mths ago
      • Reported - view

      Gray Horse It's just telling you there's a syntax error in your code. As I mentioned, my code was untested so you'll probably have to poke it a little to get it working. Maybe as a first step, change that line to:

       

      const currentMonthTransactions = allTransactions.filter(txn => {
        txn.date_next < firstDayOfNextMonth
      });

      Here's some more info on Array.filter(). Good luck!

      Reply Like
Like Follow
  • 5 mths agoLast active
  • 7Replies
  • 138Views
  • 1 Following