为 jsPDF 构建不包括空变量的数组

Building array for jsPDF not including null variables

我正在努力找出导致我什至没有看到树林中的树林的事情。我是 JavaScript 的菜鸟,我通常会使用数据库对其进行排序,但这里没有该选项。

我实际在做的是:

这是一个测验应用程序,要求用户回答一个(离散 answer/SCORM 合规)问题,然后回答另一个(免费 text/Long 文本)问题。代码的这个特定部分用于创建用户响应的 PDF 输出(带有一些 header 信息)。

我必须使用 JavaScript 构建一个 pdf(使用“jsPDF - Parallax”)输出我在其他地方生成的一组变量。例如

//VARIABLES........................................................................

var NoofQuesAnswered = VarNoofQuesAnswered.getValue();
var User_Score = VarUser_Score.getValue();
var CorrectImagData = 'data:image/jpeg;base64,/9j/etc/etc'; //Omitted actual 64base text
var IncorrectImgData = 'data:image/jpeg;base64,/9j/etc/etc'; //Omitted actual 64base text

/* General Text Values */
var notAnswered = '~~~null~~~';
var questionExcludedTitle = 'Question Excluded';
var questionAnswerGivenTitle = 'Answer Given';
var openQuestionTitle = 'Open Question';

/* Question Title - Variables */
var questionTitle_1 = 'Question 1';
var questionTitle_2 = 'Question 2';
var questionTitle_3 = 'Question 3';
var questionTitle_4 = 'Question 4';
var questionTitle_5 = 'Question 5';
var questionTitle_N = 'Question N';

/* Question Response/Answer Given - Variables */

//These variables are determined elsewhere (XML/Javascript)
var UsersAnswer_Que1 = Var_UsersAnswer_Que1.getValue(); 
var UsersAnswer_Que2 = Var_UsersAnswer_Que2.getValue();
var UsersAnswer_Que3 = Var_UsersAnswer_Que3.getValue();
var UsersAnswer_Que4 = Var_UsersAnswer_Que4.getValue();
var UsersAnswer_Que5 = Var_UsersAnswer_Que5.getValue();
var UsersAnswer_QueN = Var_UsersAnswer_QueN.getValue();

/* Open Question - Variables */

var openQuestion_1 = 'Why are young people potentially at risk?';
var openQuestion_2 = 'Why are there champions for young people?';
var openQuestion_3 = 'How does social media impact on the lives of young people?';
var openQuestion_4 = 'What kinds of support are there for young people, in your area?';
var openQuestion_5 = 'What type of information are young people most likely to need in crisis?';
var openQuestion_N = 'Open question (n)';

/* Open Question - User Response/Answer Given Variables */

//These variables are determined elsewhere (XML/Javascript)
var UserResp_LongTextQue_1 = Var_UserResp_LongTextQue_1.getValue();
var UserResp_LongTextQue_2 = Var_UserResp_LongTextQue_2.getValue();
var UserResp_LongTextQue_3 = Var_UserResp_LongTextQue_3.getValue();
var UserResp_LongTextQue_4 = Var_UserResp_LongTextQue_4.getValue();
var UserResp_LongTextQue_5 = Var_UserResp_LongTextQue_5.getValue();
var UserResp_LongTextQue_N = Var_UserResp_LongTextQue_N.getValue();

然后通过使用 jsPDF 我正在构建这样的页面,以在第一页上设置 header 信息(这是唯一具有不同内容的页面:

function genPDF() { //Function to output the pdf using jsPDF

  var doc = new jsPDF();

  /* Begin Page Build */

  // Page Header Space........................................................................

  /* There are some other logos and other header bits here but I have ommitted them due to copyright */

  doc.setFont('helvetica')

  doc.setFontSize(30)
  doc.text(50, 55, 'Results and Responses')

  doc.setFontSize(14)
  doc.text(45, 78, 'No. of Questions Answered -')
  doc.text(113, 78, NoofQuesAnswered)

  doc.text(125, 78, 'Final score -')
  doc.text(158, 78, User_Score)

  doc.line(20, 82, 195, 82)

  // Questions with responses

  /* This is the point at which i need to build the page dynamically becasue the stuff above is all static and everything below could be built dynically */

  // Question_1

  doc.line(20, 113, 195, 113)

  if (Var_UsersAnswer_Que1.equals(notAnswered)) {
    doc.setFontSize(25)
    doc.text(65, 125, questionTitle_1)
    doc.line(20, 130, 195, 130)
    doc.setFontSize(30)
    doc.text(58, 155, questionExcludedTitle)
  } else {
    doc.setFontSize(14)
    doc.text(20, 120, questionAnswerGivenTitle)
    doc.setFontSize(12)
    var splitUsersAnswer_Que1 = doc.splitTextToSize(usersAnswer_Que1, 150);
    doc.text(20, 127, splitUsersAnswer_Que1)

    doc.line(20, 130, 195, 130)

    doc.setFontSize(14)
    doc.text(20, 137, openQuestionTitle)

    doc.setFontSize(12)
    var splitOpenQuestion_1 = doc.splitTextToSize(openQuestion_1, 180);
    doc.text(20, 145, splitOpenQuestion_1)

    if (Var_UsersAnswer_Que1.isCorr('\u0041\u006C\u006C\u0020\u0061\u0072\u0065')) {
      doc.addImage(CorrectImagData, 'Correct Image', 185, 115, 10, 11)
    } else {
      doc.addImage(IncorrectImgData, 'Incorrect Image', 185, 115, 10, 11)
    }

    doc.setFontSize(12)
    var splitUserResp_LongTextQue_1 = doc.splitTextToSize(userResp_LongTextQue_1, 170);
    doc.text(20, 160, splitUserResp_LongTextQue_1)
    doc.line(20, 153, 195, 153)
  }

  // Question_2

  doc.line(20, 190, 195, 190)

  if (Var_UsersAnswer_Que2.equals(notAnswered)) {
    doc.setFontSize(25)
    doc.text(65, 202, questionTitle_2)
    doc.line(20, 207, 195, 207)
    doc.setFontSize(30)
    doc.text(58, 232, questionExcludedTitle)
  } else {
    doc.setFontSize(14)
    doc.text(20, 197, questionAnswerGivenTitle)
    doc.setFontSize(12)
    var splitUsersAnswer_Que2 = doc.splitTextToSize(usersAnswer_Que2, 150);
    doc.text(20, 204, splitUsersAnswer_Que2)

    doc.line(20, 207, 195, 207)

    doc.setFontSize(14)
    doc.text(20, 214, openQuestionTitle)

    doc.setFontSize(12)
    var splitOpenQuestion_2 = doc.splitTextToSize(openQuestion_2, 180);
    doc.text(20, 222, splitOpenQuestion_2)

    if (Var_UsersAnswer_Que2.isCorr('\u0049\u006E\u0066\u006F\u0072\u006D\u0061\u0074\u0069\u006F\u006E')) {
      doc.addImage(CorrectImagData, 'Correct Image', 185, 192, 10, 11)
    } else {
      doc.addImage(IncorrectImgData, 'Incorrect Image', 185, 192, 10, 11)
    }

    doc.setFontSize(12)
    var splitUserResp_LongTextQue_2 = doc.splitTextToSize(userResp_LongTextQue_2, 170);
    doc.text(20, 237, splitUserResp_LongTextQue_2)
    doc.line(20, 230, 195, 230)
  }

  //New Page - From here down, the position of everything on the page will be the same

  doc.addPage()

  // Question_3

  doc.line(20, 13, 195, 13)

  if (Var_UsersAnswer_Que3.equals(notAnswered)) {
    doc.setFontSize(25)
    doc.text(65, 25, questionTitle_3)
    doc.line(20, 30, 195, 30)
    doc.setFontSize(30)
    doc.text(58, 55, questionExcludedTitle)
  } else {
    doc.setFontSize(14)
    doc.text(20, 20, questionAnswerGivenTitle)
    doc.setFontSize(12)
    var splitUsersAnswer_Que3 = doc.splitTextToSize(usersAnswer_Que3, 150);
    doc.text(20, 27, splitUsersAnswer_Que3)

    doc.line(20, 30, 195, 30)

    doc.setFontSize(14)
    doc.text(20, 37, openQuestionTitle)

    doc.setFontSize(12)
    var splitOpenQuestion_3 = doc.splitTextToSize(openQuestion_3, 180);
    doc.text(20, 45, splitOpenQuestion_3)

    if (Var_UsersAnswer_Que3.isCorr('\u0041\u006C\u006C\u0020')) {
      doc.addImage(CorrectImagData, 'Correct Image', 185, 15, 10, 11)
    } else {
      doc.addImage(IncorrectImgData, 'Incorrect Image', 185, 15, 10, 11)
    }

    doc.setFontSize(12)
    var splitUserResp_LongTextQue_3 = doc.splitTextToSize(userResp_LongTextQue_3, 170);
    doc.text(20, 60, splitUserResp_LongTextQue_3)
    doc.line(20, 53, 195, 53)
  }

  // Question_4

  doc.line(20, 90, 195, 90)

  if (Var_UsersAnswer_Que4.equals(notAnswered)) {
    doc.setFontSize(25)
    doc.text(65, 102, questionTitle_4)
    doc.line(20, 107, 195, 107)
    doc.setFontSize(30)
    doc.text(58, 132, questionExcludedTitle)
  } else {
    doc.setFontSize(14)
    doc.text(20, 97, questionAnswerGivenTitle)
    doc.setFontSize(12)
    var splitUsersAnswer_Que4 = doc.splitTextToSize(usersAnswer_Que4, 150);
    doc.text(20, 104, splitUsersAnswer_Que4)

    doc.line(20, 107, 195, 107)

    doc.setFontSize(14)
    doc.text(20, 114, openQuestionTitle)

    doc.setFontSize(12)
    var splitOpenQuestion_4 = doc.splitTextToSize(openQuestion_4, 180);
    doc.text(20, 122, splitOpenQuestion_4)

    if (Var_UsersAnswer_Que4.isCorr('\u0041\u006C\u006C\u0020\u0061\u0072\u0065\u0020')) {
      doc.addImage(CorrectImagData, 'Correct Image', 185, 92, 10, 11)
    } else {
      doc.addImage(IncorrectImgData, 'Incorrect Image', 185, 92, 10, 11)
    }

    doc.setFontSize(12)
    var splitUserResp_LongTextQue_4 = doc.splitTextToSize(UserResp_LongTextQue_4, 170);
    doc.text(20, 137, splitUserResp_LongTextQue_4)
    doc.line(20, 130, 195, 130)
  }

  // Question_5

  doc.line(20, 167, 195, 167)

  if (Var_UsersAnswer_Que5.equals(notAnswered)) {
    doc.setFontSize(25)
    doc.text(65, 179, questionTitle_5)
    doc.line(20, 184, 195, 184)
    doc.setFontSize(30)
    doc.text(58, 209, questionExcludedTitle)
  } else {
    doc.setFontSize(14)
    doc.text(20, 174, questionAnswerGivenTitle)
    doc.setFontSize(12)
    var splitUsersAnswer_Que5 = doc.splitTextToSize(usersAnswer_Que5, 150);
    doc.text(20, 181, splitUsersAnswer_Que5)

    doc.line(20, 184, 195, 184)

    doc.setFontSize(14)
    doc.text(20, 191, openQuestionTitle)

    doc.setFontSize(12)
    var splitOpenQuestion_5 = doc.splitTextToSize(openQuestion_5, 180);
    doc.text(20, 199, splitOpenQuestion_5)

    if (Var_UsersAnswer_Que5.isCorr('\u0048\u006F\u0077\u0020\u0074\u006F\u0020\u0067\u0065\u0074')) {
      doc.addImage(CorrectImagData, 'Correct Image', 185, 169, 10, 11)
    } else {
      doc.addImage(IncorrectImgData, 'Incorrect Image', 185, 169, 10, 11)
    }

    doc.setFontSize(12)
    var splitUserResp_LongTextQue_5 = doc.splitTextToSize(openQuestion_5, 170);
    doc.text(20, 214, splitUserResp_LongTextQue_5)
    doc.line(20, 207, 195, 207)
  }

  //New Page - There are another 55 questions

  // doc.addPage()

  /* Question_N

  doc.line(20, 13, 195, 13)

  if (Var_UsersAnswer_QueN.equals(notAnswered)) {
   doc.setFontSize(25)
   doc.text(65, 25, questionTitle_N)
   doc.line(20, 30, 195, 30)
   doc.setFontSize(30)
   doc.text(58, 55, questionExcludedTitle)
  } else {
   doc.setFontSize(14)
   doc.text(20, 20, questionAnswerGivenTitle)
   doc.setFontSize(12)
   var splitUsersAnswer_QueN = doc.splitTextToSize(usersAnswer_QueN, 150);
   doc.text(20, 27, splitUsersAnswer_QueN)

   doc.line(20, 30, 195, 30)

   doc.setFontSize(14)
   doc.text(20, 37, openQuestionTitle)

   doc.setFontSize(12)
   var splitOpenQuestion_N = doc.splitTextToSize(openQuestion_N, 180);
   doc.text(20, 45, splitOpenQuestion_N)

   if (Var_UsersAnswer_QueN.isCorr('\u0041\u006C\u006C\u0020')) {
    doc.addImage(CorrectImagData, 'Correct Image', 185, 15, 10, 11)
   } else {
    doc.addImage(IncorrectImgData, 'Incorrect Image', 185, 15, 10, 11)
   }

   doc.setFontSize(12)
   var splitUserResp_LongTextQue_N = doc.splitTextToSize(userResp_LongTextQue_N, 170);
   doc.text(20, 60, splitUserResp_LongTextQue_N)
   doc.line(20, 53, 195, 53)
  }
 
  etc................................................
 
  */

  // Save document/Create PDF

  doc.save('Test.pdf');
}

目前,这段代码的结果是为每个问题 (1-60) 生成一个 pdf,每个问题都有一个预定义的页面位置,以及该特定问题的每个元素。然而,它会检查是否有给出的回答(在所有 60 个问题中,用户只被要求回答 30 个随机确定的问题),以及是否没有对该问题的回答(因为应用程序甚至没有提出向用户提问)它会说 "Question Excluded" 但会在页面上保留 space。

结果,我得到了 40 多页,其中只有已回答的问题显示了用户的回答,而被排除的问题则原样显示。

我希望能够使用(我认为)一个数组动态构建页面,一旦确定了尚未回答的问题然后完全忽略它们,在构建pdf 仅包含用户已回答的问题。

我想要实现的目标是根据用户回答的问题数量(可能是 1 或 2,但可能多达 30 个)减少页面数量。我无法理解的事实是,因为我必须说明每个元素在页面上的实际位置,所以我不能让它自动堆叠它们并且因为我必须使用 Javascript 我不能只查询响应然后将它们即时插入页面。

如有任何帮助,我们将不胜感激。

我可能完全走错了路,很高兴听到更好的方法。

提前致谢。

听起来您只需要一组响应,过滤为 null:

const answers = [AnswerToQuestion1, AnswerToQuestion2, AnswerToQuestion3].filter(x => x);

过滤函数删除所有为空或未定义的记录。如果你的 null 答案的等价物不是 null,而是一个字符串或其他东西,那么你可以像这样过滤掉它:

const answers = [AnswerToQuestion1, AnswerToQuestion2, AnswerToQuestion3].filter(x => x != whateverYourNullLooksLike);