我如何修复这个 Airtable 脚本中的错误,该脚本计算一个季度的剩余时间以达到一个数字?

问题描述 投票:0回答:0

我正在尝试为执行以下操作的 Airtable 扩展编写个人脚本:

  1. 计算季度写作目标的进度: 这对特定视图中字段的数值求和。

  2. 我要写的剩余金额: 这不是数字 在我的基地可用。

  3. 返回我每周需要写多少篇文章才能及时实现我的目标。

  4. 返回每篇文章需要的weekdays数 剩下来及时实现目标。

到目前为止,这是脚本:


/*This function does the following: 
1. Calculates total articles in a view
2. Calculates progress towards quarterly goal
3. Returns articles left to achieve goal */

let table = base.getTable("Writing Assignments");
let view = table.getView("My Production Count");

// count the articles and calculate percentage of quarterly goal:
let result = await view.selectRecordsAsync({ fields: ["Article count"] });
let runningTotal = 0;
let minGoal = 18;
let maxGoal = 24;
for (let record of result.records) {
  // this calculates percentage of total and rounds to the nearest integer:
  runningTotal += record.getCellValue("Article count");
}
const progress = Math.round((runningTotal * 100) / minGoal);

// calculate articles remaining:
var remaining = minGoal - runningTotal;



//This function counts the time remaining in the quarter

var weeks;
var days;
var quarter;

/* 
  Now we write a function that:
    1. Figures out the current date
    2. Compares that to the end date of each quarter, to figure out what quarter we're in
    3. Updates those empty spots in the page with the time left in the current quarter
*/

const getTimeUntilEndOfQuarter = () => {
  /* Here's the current time */
  const now = new Date();

  /* 
    Here's the end date of each quarter.
    Notice that I'm using the current year from "now",
    so we won't have to worry about doing any manual updating each year.
  */
  const Q1 = new Date("April 1 " + now.getFullYear());
  const Q2 = new Date("July 1 " + now.getFullYear());
  const Q3 = new Date("October 1 " + now.getFullYear());
  const Q4 = new Date("January 1 " + (now.getFullYear() + 1));

  /* 
    Now let's compare the current time to each of those end dates.
    Once we've found the quarter we're in, update the page to say which one,
    then update the "currentQuarter" value to be that quarter's end date.
  */
  let currentQuarter;
  if (now > Q3) {
    currentQuarter = Q4;
    quarter = "Q4";
  } else if (now > Q2) {
    currentQuarter = Q3;
    quarter = "Q3";
  } else if (now > Q1) {
    currentQuarter = Q2;
    quarter = "Q2";
  } else {
    currentQuarter = Q1;
    quarter = "Q1";
  }

  /* 
    Here, we get the number of milliseconds between now and the end of the quarter,
    then we do some math to get that in days instead, 
    because that'll be a number that actually makes sense.
  */
  const timeLeft = currentQuarter.getTime() - now.getTime();
  var totalDaysLeft = Math.ceil(timeLeft / (1000 * 3600 * 24));

  /*
    Divide that number by 7 and round down to get the number of weeks,
    then get the modulo of that number and the total number of days to get the remaining days
  */
  var weeksLeft = Math.floor(totalDaysLeft / 7);
  var daysLeft = totalDaysLeft % 7;

  // Remove weekends

  var totalWeekdaysLeft = function subtractWeekends(totalDaysLeft) {
    days -= weeksLeft * 2;

    // Handle special cases
    var startDay = now.getDay();
    var endDay = currentQuarter.getDay();

    // Remove weekend not previously removed.   
    if (startDay - endDay > 1) {
      days -= 2;
    }
    // Remove start day if span starts on Sunday but ends before Saturday
    if (startDay == 0 && endDay != 6) {
      days--;
    }
    // Remove end day if span ends on Saturday but starts after Sunday
    if (endDay == 6 && startDay != 0) {
      days--;
    }
  }


  // Calculate articles to submit per week and days needed to write each article to achieve goal:

  var daysPerArticle = totalWeekdaysLeft / remaining;
  var articlesPerWeek = weeksLeft / remaining;




  /*
    Now let's get those numbers on the page
  */

  /*
    If the number isn't 1, make sure we say "weeks" or "days" plural
  */


  const weeksPlural = weeksLeft == 1 ? "week" : "weeks";
  const daysPlural = totalWeekdaysLeft == 1 ? "day" : "days";
};



/* 
  Remember that function we just wrote? Run it! 
*/
getTimeUntilEndOfQuarter();

/*
  And run it again every 15 minutes
*/


output.markdown(`
# Status

## Progress toward quarterly goal:  

![](https://geps.dev/progress/${progress})

${progress} % to ${minGoal} articles

## Days per article to hit goal:
${daysPerArticle} ${daysPlural} per article 

## Articles per week to hit goal:
${articlesPerWeek} ${weeksPlural} per week 

`);

我遇到的问题: 看来我声明变量的方式似乎使我试图计算的数学变得不可能,因为它不断返回错误。

我是 JavaScript 的新手,所以下面的一些代码是基于修改 Airtable 的示例,一些朋友帮助我动态计算一个季度剩余的天数和周数的代码,以及一些 Stack Overflow 示例。

我搞砸的事情可能更多,但我还没有足够的知识来了解。 😎提前感谢您的帮助!

javascript javascript-objects variable-assignment airtable
© www.soinside.com 2019 - 2024. All rights reserved.