Acrobat Javascript 填写表格并将其保存到许多文件夹之一

Acrobat Javascript that fills out form and saves it to one of many folders

编辑:我在其他论坛的帮助下解决了我的问题。这是我的变量,我从不让 while 循环定义它们,我只是在顶部声明它们,所以循环只是将其视为常量值,这是导入的第一条记录。

所以,我正在尝试填写我用 txt 文件中的信息制作的 Acrobat PDF 表格,然后根据表格中的信息将其保存到多个文件夹之一。我有一系列以数字范围命名的文件夹设置(即 1-49、50-99、100-149 等),一些文件夹前面有字母 "z",因为我们的编号系统是我们的're tracking with these forms includes some names formatted with a Z at the end(我之前的人有点烂)。

我正在尝试更正的代码的逻辑是这样的(如果您只想查看下面的代码):

在 while 循环中导入文本数据 -> 使用 indexOf 搜索字母 Z 的目标字段值 -> 使用 switch 设置 2 个代码块,1 个用于包含 "z" 的值,1 个用于没有 "z" 的值 -> 根据 "z" 确定哪个代码块到 运行 -> 使用 substr(0,3) 从值中提取前三个字符并在代码块与我们的数字范围进行比较以查看数字属于哪个范围 -> 根据它属于哪个范围(即“100-149/”)设置一个带有字符串的变量 -> 去保存 PDF -> 使用先前设置的变量修改 saveAs 的输出目录,将我们的变量切换到哪个范围,并将 PDF 保存在名为数字范围的相应文件夹中 -> 增加一个变量并重复循环

我现在的问题是,我们使用开关代码设置的变量输出时仅为零。因此,它没有添加我的字符​​串并让它在正确的子文件夹中创建 PDF,它只是在文件名的开头添加一个 0 并将其保存在顶级文件夹中。我以为我在 switch 部分的逻辑是正确的,但也许我对 JS 不够熟悉,看不到我的问题。

代码: ImportES = app.trustedFunction(函数() {

var fileName = "/c/Users/jcoleman/Desktop/Endless Slings/Endless Sling Data.txt";
var outputDir = "/c/Users/jcoleman/Desktop/Endless Slings/";
var whichCert = this.getField("PFFSID").value;
var firstThree = whichCert.substr(0,3);
var whichFolder = "0";
var whichCase = whichCert.indexOf("Z");

var err = 0;
var idx = 0;
while (err == 0) {
err = this.importTextData(fileName, idx);

switch (whichCase) {

case 3: {
    if (0 < firstThree && firstThree <49)
        whichFolder = "z1-49/";
    else if (49 < firstThree && firstThree <99)
        whichFolder = "z50-99/";
    else if (99 < firstThree && firstThree <149) 
        whichFolder = "z100-149/";
    else if (149 < firstThree && firstThree <199) 
        whichFolder = "z150-199/";
    else if (199 < firstThree && firstThree <249) 
        whichFolder = "z200-249/";
    else if (249 < firstThree && firstThree <299) 
        whichFolder = "z250-299/";
    else if (299 < firstThree && firstThree <349) 
        whichFolder = "z300-349/";
    else if (349 < firstThree && firstThree <399) 
        whichFolder = "z350-399/";
    else if (399 < firstThree && firstThree <449) 
        whichFolder = "z400-449/";
    else if (449 < firstThree && firstThree <499) 
        whichFolder = "z450-499/";
    else if (499 < firstThree && firstThree <549) 
        whichFolder = "z500-549/";
    else if (549 < firstThree && firstThree <599) 
        whichFolder = "z550-599/";
    else if (599 < firstThree && firstThree <649) 
        whichFolder = "z600-649/";
    else if (649 < firstThree && firstThree <699) 
        whichFolder = "z650-699/";
    else if (699 < firstThree && firstThree <749) 
        whichFolder = "z700-749/";
    else if (749 < firstThree && firstThree <799) 
        whichFolder = "z750-799/";
    else if (799 < firstThree && firstThree <849) 
        whichFolder = "z800-849/";
    else if (849 < firstThree && firstThree <899) 
        whichFolder = "z850-899/";
    else if (899 < firstThree && firstThree <949) 
        whichFolder = "z900-949/";
    else if (949 < firstThree && firstThree <999) 
        whichFolder = "z950-999/";
    break;
}



case -1: {
    if (0 < firstThree && firstThree <49)
        whichFolder = "1-49/";
    else if (49 < firstThree && firstThree <99)
        whichFolder = "50-99/";
    else if (99 < firstThree && firstThree <149) 
        whichFolder = "100-149/";
    else if (149 < firstThree && firstThree <199) 
        whichFolder = "150-199/";
    else if (199 < firstThree && firstThree <249) 
        whichFolder = "200-249/";
    else if (249 < firstThree && firstThree <299) 
        whichFolder = "250-299/";
    else if (299 < firstThree && firstThree <349) 
        whichFolder = "300-349/";
    else if (349 < firstThree && firstThree <399) 
        whichFolder = "350-399/";
    else if (399 < firstThree && firstThree <449) 
        whichFolder = "400-449/";
    else if (449 < firstThree && firstThree <499) 
        whichFolder = "450-499/";
    else if (499 < firstThree && firstThree <549) 
        whichFolder = "500-549/";
    else if (549 < firstThree && firstThree <599) 
        whichFolder = "550-599/";
    else if (599 < firstThree && firstThree <649) 
        whichFolder = "600-649/";
    else if (649 < firstThree && firstThree <699) 
        whichFolder = "650-699/";
    else if (699 < firstThree && firstThree <749) 
        whichFolder = "700-749/";
    else if (749 < firstThree && firstThree <799) 
        whichFolder = "750-799/";
    else if (799 < firstThree && firstThree <849) 
        whichFolder = "800-849/";
    else if (849 < firstThree && firstThree <899) 
        whichFolder = "850-899/";
    else if (899 < firstThree && firstThree <949) 
        whichFolder = "900-949/";
    else if (949 < firstThree && firstThree <999) 
        whichFolder = "950-999/";
}
}



if (err == -1)
    app.alert("Error: Cannot Open File");
else if (err == -2)
    app.alert("Error: Cannot Load Data");
else if (err == 1)
    app.alert("Warning: User Cancelled File Select");
else if (err == 2)
    app.alert("Warning: User Cancelled Row Select");
else if (err == 3)
    app.alert("Warning: Missing Data");
else if (err == 0) {
    this.saveAs(outputDir + whichFolder + "ES-" + this.getField("PFFSID").value + ".pdf"); 
    idx++;
}
}
});

// menu item
app.addMenuItem({
cName: "ImportES",
cUser: "Import Endless Slings",
cParent: "Edit",
cExec: "ImportES()",
cEnable: "event.rc = (event.target != null);",
nPos: 1
});

您似乎遇到了上下文问题。没有你的文件我无法测试这个,但这是我认为应该有效的...

var ImportES = app.trustedFunction(function(doc) {
    var fileName = "/c/Users/jcoleman/Desktop/Endless Slings/Endless Sling Data.txt";
    var outputDir = "/c/Users/jcoleman/Desktop/Endless Slings/";
    var whichCert = doc.getField("PFFSID").value;
    var firstThree = whichCert.substr(0,3);
    var whichFolder = "0";
    var whichCase = whichCert.indexOf("Z");

    var err = 0;
    var idx = 0;
    while (err == 0) {
    err = doc.importTextData(fileName, idx);

    switch (whichCase) {

    case 3: {
        if (0 < firstThree && firstThree <49)
            whichFolder = "z1-49/";
        else if (49 < firstThree && firstThree <99)
            whichFolder = "z50-99/";
        else if (99 < firstThree && firstThree <149) 
            whichFolder = "z100-149/";
        else if (149 < firstThree && firstThree <199) 
            whichFolder = "z150-199/";
        else if (199 < firstThree && firstThree <249) 
            whichFolder = "z200-249/";
        else if (249 < firstThree && firstThree <299) 
            whichFolder = "z250-299/";
        else if (299 < firstThree && firstThree <349) 
            whichFolder = "z300-349/";
        else if (349 < firstThree && firstThree <399) 
            whichFolder = "z350-399/";
        else if (399 < firstThree && firstThree <449) 
            whichFolder = "z400-449/";
        else if (449 < firstThree && firstThree <499) 
            whichFolder = "z450-499/";
        else if (499 < firstThree && firstThree <549) 
            whichFolder = "z500-549/";
        else if (549 < firstThree && firstThree <599) 
            whichFolder = "z550-599/";
        else if (599 < firstThree && firstThree <649) 
            whichFolder = "z600-649/";
        else if (649 < firstThree && firstThree <699) 
            whichFolder = "z650-699/";
        else if (699 < firstThree && firstThree <749) 
            whichFolder = "z700-749/";
        else if (749 < firstThree && firstThree <799) 
            whichFolder = "z750-799/";
        else if (799 < firstThree && firstThree <849) 
            whichFolder = "z800-849/";
        else if (849 < firstThree && firstThree <899) 
            whichFolder = "z850-899/";
        else if (899 < firstThree && firstThree <949) 
            whichFolder = "z900-949/";
        else if (949 < firstThree && firstThree <999) 
            whichFolder = "z950-999/";
        break;
    }



    case -1: {
        if (0 < firstThree && firstThree <49)
            whichFolder = "1-49/";
        else if (49 < firstThree && firstThree <99)
            whichFolder = "50-99/";
        else if (99 < firstThree && firstThree <149) 
            whichFolder = "100-149/";
        else if (149 < firstThree && firstThree <199) 
            whichFolder = "150-199/";
        else if (199 < firstThree && firstThree <249) 
            whichFolder = "200-249/";
        else if (249 < firstThree && firstThree <299) 
            whichFolder = "250-299/";
        else if (299 < firstThree && firstThree <349) 
            whichFolder = "300-349/";
        else if (349 < firstThree && firstThree <399) 
            whichFolder = "350-399/";
        else if (399 < firstThree && firstThree <449) 
            whichFolder = "400-449/";
        else if (449 < firstThree && firstThree <499) 
            whichFolder = "450-499/";
        else if (499 < firstThree && firstThree <549) 
            whichFolder = "500-549/";
        else if (549 < firstThree && firstThree <599) 
            whichFolder = "550-599/";
        else if (599 < firstThree && firstThree <649) 
            whichFolder = "600-649/";
        else if (649 < firstThree && firstThree <699) 
            whichFolder = "650-699/";
        else if (699 < firstThree && firstThree <749) 
            whichFolder = "700-749/";
        else if (749 < firstThree && firstThree <799) 
            whichFolder = "750-799/";
        else if (799 < firstThree && firstThree <849) 
            whichFolder = "800-849/";
        else if (849 < firstThree && firstThree <899) 
            whichFolder = "850-899/";
        else if (899 < firstThree && firstThree <949) 
            whichFolder = "900-949/";
        else if (949 < firstThree && firstThree <999) 
            whichFolder = "950-999/";
    }
}



if (err == -1)
    app.alert("Error: Cannot Open File");
else if (err == -2)
    app.alert("Error: Cannot Load Data");
else if (err == 1)
    app.alert("Warning: User Cancelled File Select");
else if (err == 2)
    app.alert("Warning: User Cancelled Row Select");
else if (err == 3)
    app.alert("Warning: Missing Data");
else if (err == 0) {
    this.saveAs(outputDir + whichFolder + "ES-" + this.getField("PFFSID").value + ".pdf"); 
    idx++;
}
}
});

// menu item
app.addMenuItem({
cName: "ImportES",
cUser: "Import Endless Slings",
cParent: "Edit",
cExec: "ImportES(this)",
cEnable: "event.rc = (event.target != null);",
nPos: 1
});

我在其他论坛的帮助下解决了我的问题。这是我的变量,我从不让 while 循环定义它们,我只是在顶部声明它们,所以循环只是将它视为一个常量值,这是导入的第一条记录。

大家保持均衡的饮食,不要忽视水果和变数。