📄 aform.js
字号:
}
info = AFExtractRegExp(AFAMRegExp, string);
if(info)
{
string = info[0];
}
info = AFExtractRegExp(AFTimeLongRegExp, string);
if(info)
{
info[1] += pm;
return info;
}
info = AFExtractRegExp(AFTimeShortRegExp, string);
if(info)
{
info[1] += pm;
return info;
}
return null;
}
function AFGetMonthIndex(string)
{ /* attempts to identify the given string as a month or a valid abbreviation,
* it expects the given string to be the valid month from the matced RegExp.
* returns the month index (January = 1) or zero on failure */
var monthre = new RegExp(string + "\\[(\\d+)\\]", "i");
var result = monthre.exec(IDS_MONTH_INFO);
if(string && result) return 1.0 * result[1];
return 0;
}
function AFMatchMonth(string)
{ /* attempts to find a valid month embedded in a string; returns the month
* index (January = 1) or zero on failure */
for(var it = 0; it < AFMonthsRegExp.length; it++)
if(AFMonthsRegExp[it].test(string))
return AFGetMonthIndex(RegExp.lastMatch);
return 0;
}
function AFGetMonthString(index)
{ /* returns the string corresponding to the given month or a string that
* is indicative of the fact that the index was invalid */
var monthre = new RegExp("(\\w+)\\[" + index + "\\]");
var result = monthre.exec(IDS_MONTH_INFO);
if(result) return result[1];
return IDS_INVALID_MONTH;
}
function AFParseTime(string, date)
{ /* attempts to parse a string containing a time; returns null on failure
* or a Date object on success. Time can be in ugly format. */
var pm, am;
var nums = AFExtractNums(string);
if (!date)
date = new Date();
var hour, minutes, seconds;
if(!string) return date;
if(!nums) return null;
if(nums.length < 2 || nums.length > 3) return null;
if(AFPMRegExp.test(string)) pm = true;
else pm = false;
if(AFAMRegExp.test(string)) am = true;
else am = false;
hour = new Number(nums[0]); /* force it to number */
if(pm)
{
if(hour < 12) hour += 12;
}
else if (am)
{
if(hour >= 12) hour -= 12;
}
minutes = nums[1];
if(nums.length == 3) seconds = nums[2];
else seconds = 0;
date.setHours(hour);
date.setMinutes(minutes);
date.setSeconds(seconds);
if(date.getHours() != hour)
return null;
if(date.getMinutes() != minutes)
return null;
if(date.getSeconds() != seconds)
return null;
return date;
}
function AFDateFromYMD(nYear, nMonth, nDate)
{ /* Validates the given fields and returns a date based on them */
var dDate = new Date();
dDate.setFullYear(nYear, nMonth, nDate);
if(dDate.getFullYear() != nYear)
return null;
if(dDate.getMonth() != nMonth)
return null;
if(dDate.getDate() != nDate)
return null;
return dDate;
}
function AFParseDateEx(cString, cOrder)
{ /* Attempts to parse a string containing some form of date; returns null
** on failure or a Date object on success. cOrder should be the order in
** which the date is entered (e.g. ymd, mdy, etc.). Use AFParseDateOrder to
** generate this string from an arbitrary format string. */
var nYear;
var nMonth;
var nDate;
var nYCount;
var dDate = new Date();
dDate.setHours(12, 0, 0);
/* Empty string returns current date/time. */
if (!cString) {
return dDate;
}
nYCount = AFParseDateYCount(cOrder); /* count the number of digits for year in the selected format */
cOrder = AFParseDateOrder(cOrder); /* make sure its in the "ymd" format */
/* Extract any time information in the string. */
var info = AFExtractTime(cString);
if (info)
cString = info[0];
/* Break down the date into an array of numbers. */
var aNums = AFExtractNums(cString);
if(!aNums)
return null; /* No numbers? */
/* User supplied three numbers. */
if (aNums.length == 3) {
nYear = 1.0 * aNums[cOrder.indexOf("y")];
if (nYCount > 2 && nYear < 100)
return null; /* must enter 4 digits for the year to match with the format of the field */
nYear = AFDateHorizon(nYear);
dDate = AFDateFromYMD(nYear, aNums[cOrder.indexOf("m")] - 1, aNums[cOrder.indexOf("d")]);
if (info)
dDate = AFParseTime(info[1], dDate);
return dDate;
}
/* Find text based month, if supplied. */
nMonth = AFMatchMonth(cString);
/* User supplied two numbers. */
if(aNums.length == 2) {
if (nMonth) {
/* Easy case, the month was text and we have two numbers. */
if (cOrder.indexOf("y") < cOrder.indexOf("d")) {
nYear = 1.0 * aNums[0];
nDate = aNums[1];
} else {
nYear = 1.0 * aNums[1];
nDate = aNums[0];
}
if (nYCount > 2 && nYear < 100)
return null; /* must enter 4 digits for the year to match with the format of the field */
nYear = AFDateHorizon(nYear);
dDate = AFDateFromYMD(nYear, nMonth - 1, nDate);
if (info)
dDate = AFParseTime(info[1], dDate);
return dDate;
}
/* More difficult case. We have two numbers and three slots, how
** to allocate them? */
if (cOrder.indexOf("y") < cOrder.indexOf("d")) {
/* Year comes before date and as such we allocate the two
** numbers to the month and the year only. */
if (cOrder.indexOf("y") < cOrder.indexOf("m")) {
nYear = 1.0 * aNums[0];
nMonth = aNums[1];
} else {
nYear = 1.0 * aNums[1];
nMonth = aNums[0];
}
if (nYCount > 2 && nYear < 100)
return null; /* must enter 4 digits for the year to match with the format of the field */
nYear = AFDateHorizon(nYear);
dDate = AFDateFromYMD(nYear, nMonth - 1, 1);
} else {
/* Date comes before year and so we allocate the two numbers
** to the date and the month only. */
nYear = dDate.getFullYear();
if (cOrder.indexOf("d") < cOrder.indexOf("m")) {
dDate = AFDateFromYMD(nYear, aNums[1] - 1, aNums[0]);
} else {
dDate = AFDateFromYMD(nYear, aNums[0] - 1, aNums[1]);
}
}
if (info)
dDate = AFParseTime(info[1], dDate);
return dDate;
}
/* User supplied one number. */
if(aNums.length == 1) {
if (nMonth) {
/* We have one number and two slots (y/d) and need to allocate
** them based on who came first in the format. */
if(cOrder.indexOf("y") < cOrder.indexOf("d")) {
nYear = 1.0 * aNums[0];
if (nYCount > 2 && nYear < 100)
return null; /* must enter 4 digits for the year to match with the format of the field */
nYear = AFDateHorizon(nYear);
dDate = AFDateFromYMD(nYear, nMonth - 1, 1);
} else {
nYear = dDate.getFullYear();
dDate = AFDateFromYMD(nYear, nMonth - 1, aNums[0]);
}
if (info)
dDate = AFParseTime(info[1], date);
return dDate;
}
/* We have one number and three slots and need to allocate them
** based on who came first in the format. */
nYear = dDate.getFullYear();
nMonth = dDate.getMonth();
nDate = dDate.getDate();
switch (cOrder.charAt(0)) {
case "y":
nYear = 1.0 * aNums[0];
if (nYCount > 2 && nYear < 100)
return null; /* must enter 4 digits for the year to match with the format of the field */
nYear = AFDateHorizon(nYear);
break;
case "m":
nMonth = aNums[0] - 1;
break;
case "d":
nDate = aNums[0];
break;
}
dDate = AFDateFromYMD(nYear, nMonth, nDate);
if (info)
dDate = AFParseTime(info[1], date);
return dDate;
}
/* No idea how to deal with the other combinations. */
return null;
}
function AFDateHorizon(nYear)
{ /* Takes the year supplied and applies the date horizon heuristic.
** All years between 50 and 100 we add 1900. All years less than 50 we add 2000. */
if (nYear < 100 && nYear >= 50) {
nYear += 1900;
} else if (nYear >= 0 && nYear < 50) {
nYear += 2000;
}
return nYear;
}
function AFParseDate(string, longEntry, shortEntry, wordMonthEntry, monthYearEntry)
{ /* OBSOLETE: Use AFParseDateEx instead. */
var nums;
var year, month;
var date;
var info = AFExtractTime(string);
if(!string) return new Date();
if(info)
string = info[0];
date = new Date();
nums = AFExtractNums(string);
if(!nums) return null;
if(nums.length == 3)
{
year = 1.0 * nums[eval(longEntry.charAt(0))];
year = AFDateHorizon(year);
date = AFDateFromYMD(year, nums[eval(longEntry.charAt(1))] - 1, nums[eval(longEntry.charAt(2))]);
if (info)
date = AFParseTime(info[1], date);
return date;
}
month = AFMatchMonth(string);
if(nums.length == 2)
{
if(month)
{
year = 1.0 * nums[eval(wordMonthEntry.charAt(0))];
year = AFDateHorizon(year);
date = AFDateFromYMD(year, month - 1, nums[eval(wordMonthEntry.charAt(1))]);
if (info)
date = AFParseTime(info[1], date);
return date;
}
if(monthYearEntry)
{
year = 1.0 * nums[eval(monthYearEntry.charAt(0))];
year = AFDateHorizon(year);
date = AFDateFromYMD(year, nums[eval(monthYearEntry.charAt(1))] - 1, 1);
}
else
date = AFDateFromYMD(date.getFullYear(),
nums[eval(shortEntry.charAt(0))] - 1,
nums[eval(shortEntry.charAt(1))]);
if (info)
date = AFParseTime(info[1], date);
return date;
}
if(month && nums.length == 1)
{
if(monthYearEntry)
{
year = 1.0 * nums[0];
year = AFDateHorizon(year);
date = AFDateFromYMD(year, month - 1, 1);
}
else
date = AFDateFromYMD(date.getFullYear(), month - 1, nums[0]);
if (info)
date = AFParseTime(info[1], date);
return date;
}
return null;
}
function AFParseDateWithPDF(value, pdf)
{ /* OBSOLETE: Use AFParseDateEx instead. */
var cOldFormats = new Array(
"m/d", "m/d/yy", "mm/dd/yy", "mm/yy", "d-mmm", "d-mmm-yy", "dd-mmm-yy",
"yy-mm-dd", "mmm-yy", "mmmm-yy", "mmm d, yyyy", "mmmm d, yyyy",
"m/d/yy h:MM tt", "m/d/yy HH:MM" );
return AFParseDateEx(value, cOldFormats[pdf]);
}
function AFMergeChange(event)
{ /* merges the last change with the uncommitted change */
var prefix, postfix;
var value = event.value;
if(event.willCommit) return event.value;
if(event.selStart >= 0)
prefix = value.substring(0, event.selStart);
else prefix = "";
if(event.selEnd >= 0 && event.selEnd <= value.length)
postfix = value.substring(event.selEnd, value.length);
else postfix = "";
return prefix + event.change + postfix;
}
function AFRange_Validate(bGreaterThan, nGreaterThan, bLessThan, nLessThan)
{ /* This function validates the current event to ensure that its value is
** within the specified range. */
var cError = "";
if (event.value == "")
return;
if (bGreaterThan && bLessThan) {
if (event.value < nGreaterThan || event.value > nLessThan)
cError = util.printf(IDS_GT_AND_LT, nGreaterThan, nLessThan);
} else if (bGreaterThan) {
if (event.value < nGreaterThan)
cError = util.printf(IDS_GREATER_THAN, nGreaterThan);
} else if (bLessThan) {
if (event.value > nLessThan)
cError = util.printf(IDS_LESS_THAN, nLessThan);
}
if (cError != "") {
app.alert(cError, 0);
event.rc = false;
}
}
function AFSimpleInit(cFunction)
{ /* Convenience function used by AFSimple_Calculate. */
switch (cFunction)
{
case "PRD":
return 1.0;
break;
}
return 0.0;
}
function AFSimple(cFunction, nValue1, nValue2)
{ /* Convenience function used by AFSimple_Calculate. */
var nValue = 1.0 * nValue1;
/* Have to do this otherwise JavaScript thinks it's dealing with strings. */
nValue1 = 1.0 * nValue1;
nValue2 = 1.0 * nValue2;
switch (cFunction)
{
case "AVG":
case "SUM":
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -