将Excel公式转换为C.

时间:2013-08-20 23:12:17

标签: c excel-formula

我正在尝试将以下公式转换为C但未能获得正确的语法。基本上,我使用今天的日期减去一组定义的日期来查看mod余数是否与28天的轮换相匹配,如果为真,则提供所需的答案。下面是我正在尝试转换为C ...的工作Excel公式

=IF(OR(
MOD(TODAY()-DATE(2013,1,7),28)=0,
MOD(TODAY()-DATE(2013,1,16),28)=0,
MOD(TODAY()-DATE(2013,1,25),28)=0,
MOD(TODAY()-DATE(2013,2,4),28)=0,
MOD(TODAY()-DATE(2013,2,13),28)=0,
MOD(TODAY()-DATE(2013,2,22),28)=0)=TRUE,"Option1",IF(
OR(
MOD(TODAY()-DATE(2013,1,9),28)=0,
MOD(TODAY()-DATE(2013,1,18),28)=0,
MOD(TODAY()-DATE(2013,1,28),28)=0,
MOD(TODAY()-DATE(2013,2,6),28)=0,
MOD(TODAY()-DATE(2013,2,15),28)=0,
MOD(TODAY()-DATE(2013,2,25),28)=0)=TRUE,"Option2",IF(
OR(
MOD(TODAY()-DATE(2013,1,11),28)=0,
MOD(TODAY()-DATE(2013,1,21),28)=0,
MOD(TODAY()-DATE(2013,1,30),28)=0,
MOD(TODAY()-DATE(2013,2,8),28)=0,
MOD(TODAY()-DATE(2013,2,18),28)=0,
MOD(TODAY()-DATE(2013,2,27),28)=0)=TRUE,"Option3",IF(
OR(
MOD(TODAY()-DATE(2013,1,14),28)=0,
MOD(TODAY()-DATE(2013,1,23),28)=0,
MOD(TODAY()-DATE(2013,2,1),28)=0,
MOD(TODAY()-DATE(2013,2,11),28)=0,
MOD(TODAY()-DATE(2013,2,20),28)=0,
MOD(TODAY()-DATE(2013,3,1),28)=0)=TRUE,"Option4","Not Required"))))

善良的灵魂能帮助我朝正确的方向前进吗?

2 个答案:

答案 0 :(得分:1)

我认为这可能是相关的:

http://www.codeproject.com/Articles/2750/Excel-serial-date-to-Day-Month-Year-and-vise-versa

转换后的代码如下

#include <time.h>
#include <stdio.h>

int DMYToExcelSerialDate(int nYear, int nMonth, int nDay)
{
// Excel/Lotus 123 have a bug with 29-02-1900. 1900 is not a
// leap year, but Excel/Lotus 123 think it is...
if (nDay == 29 && nMonth == 02 && nYear==1900)
    return 60;

// DMY to Modified Julian calculatie with an extra substraction of 2415019.
long nSerialDate = 
        (int)(( 1461 * ( nYear + 4800 + (int)(( nMonth - 14 ) / 12) ) ) / 4) +
        (int)(( 367 * ( nMonth - 2 - 12 * ( ( nMonth - 14 ) / 12 ) ) ) / 12) -
        (int)(( 3 * ( (int)(( nYear + 4900 + (int)(( nMonth - 14 ) / 12) ) / 100) ) ) / 4) +
        nDay - 2415019 - 32075;

if (nSerialDate < 60)
{
    // Because of the 29-02-1900 bug, any serial date 
    // under 60 is one off... Compensate.
    nSerialDate--;
}

return (int)nSerialDate;
}

int main (void)
{
int serial_date;
time_t now;
struct tm *date_time;

time (&now);

date_time = localtime(&now);
serial_date = DMYToExcelSerialDate( (date_time->tm_year+1900), (date_time->tm_mon + 1) ,date_time->tm_mday);


if ( ( (serial_date - DMYToExcelSerialDate(2013,1,7))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,1,16))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,1,25))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,4))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,13))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,22))%28 == 0) )
{
    printf("option1\n");
    return 1;
}
else if ( ( (serial_date - DMYToExcelSerialDate(2013,1,9))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,1,18))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,1,28))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,6))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,15))%28 == 0) ||
    ( (serial_date - DMYToExcelSerialDate(2013,2,25))%28 == 0) )
{
   printf("option2\n");
   return 2;
}
else if ( ( (serial_date - DMYToExcelSerialDate(2013,1,11))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,1,21))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,1,30))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,8))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,18))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,27))%28 == 0) )
{
    printf("option3\n");
    return 3;
}
else if ( ( (serial_date - DMYToExcelSerialDate(2013,1,14))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,1,23))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,1))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,11))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,2,20))%28 == 0) ||
     ( (serial_date - DMYToExcelSerialDate(2013,3,1))%28 == 0) )
{
    printf("option4\n");
    return 4;
}
else
{
    printf("not required\n");
    return 0;
}

}

请注意,转换后的代码使用了根据“共同开发和分发许可证”(CDDL)许可的源代码,如上文所述。

答案 1 :(得分:0)

我不是Excel函数向导,但我非常确定以下三个函数 可能会有所帮助:time(),localtime()和mktime()。每个都是ANSI C并有详细记录。通过获取当前时间,然后在函数之间来回传递(通过指向struct或time_t值的指针),您可以操纵或检查不同的时间值。 简单示例:

#include <windows.h>
#include <ansi_c.h>

void timeFunc(void);

void main(void)
{
    timeFunc();
}

void timeFunc(void)
{
    struct  tm *today;
    struct tm *modified_time;
    time_t  secCntGMT1900 = 0; 
    time_t  modifiedTime;
    // Get GMT time in seconds since 1900
    time( &secCntGMT1900 );

    // Convert GMT time to local time to local time format
    today = localtime( &secCntGMT1900 );

    //modify 'today' members:
    today->tm_hour = 0;
    today->tm_min = 0;
    today->tm_sec = 0;
    today->tm_mon = 2;
    today->tm_mday =1;
    today->tm_year;

    //modified struct passed to mktime()
    modifiedTime = mktime(today);   
}