结合JavaScript if语句进行剪纸

时间:2018-10-03 12:26:30

标签: javascript

我的任务是在Js中为剪纸脚本组合两个if语句。它是一个打印管理软件。我拥有我所需的一切,我相信下面的脚本。我认为问题在于将这两个“如果”合并为一个陈述。我不熟悉Javascript,也不熟悉python。我希望在重新整理此脚本方面有所帮助,如下所述。

PaperCut print script API reference

目标:

如果打印作业超过10页,则仅执行费用中心弹出窗口,否则仅将其自动记入公司的非计费(ADM-3900)帐户。 如果作业超过50页,请将其从HP重定向到较大的复印机。在这种情况下,从test_printer3到“复印机–颜色”。

/*
* Redirect large jobs without confirmation
* 
* Users printing jobs larger than the defined number of pages have their jobs 
* automatically redirected to another printer or virtual queue.
* This can be used to redirect large jobs from slower or high cost printers 
* to more efficient or faster high volume printers.
*/

function printJobHook(inputs, actions) {

  /*
  * This print hook will need access to all job details
  * so return if full job analysis is not yet complete.
  * The only job details that are available before analysis
  * are metadata such as username, printer name, and date.
  *
  * See reference documentation for full explanation.
  */

  /*
  * NOTE: The high-volume printer must be compatible with the source printer.
  *       i.e. use the same printer language like PCL or Postscript.
  *       If this is a virtual queue, all printers in the queue must use
  *       the same printer language.
  */

  if (!inputs.job.isAnalysisComplete) {
    // No job details yet so return.

    return;
    actions.job.chargeToPersonalAccount();

    return;


    if (inputs.job.totalPages < 10) {

      // Charge to the firm non-bill account

      actions.job.chargeToSharedAccount(ADM-3900);

    } 
    // Account Selection will still show

  } 

  var LIMIT             = 5; // Redirect jobs over 5 pages.


  var HIGH_VOL_PRINTER  = "Copier - Color";

  if (inputs.job.totalPages > LIMIT) {
    /*
    * Specify actions.job.bypassReleaseQueue() if you wish to bypass the release queue
    * on the original printer the job was sent to.  (Otherwise if held at the target,
    * the job will need to be released from two different queues before it will print.)
    */
    actions.job.bypassReleaseQueue();

    /*
    * Job is larger than our page limit, so redirect to high-volume printer,
    * and send a message to the user.
    * Specify "allowHoldAtTarget":true to allow the job to be held at the hold/release
    * queue for the high-volume printer, if one is defined.
    */

    actions.job.redirect(HIGH_VOL_PRINTER, {allowHoldAtTarget: true});

    // Notify the user that the job was automatically redirected.
    actions.client.sendMessage(
      "The print job was over " + LIMIT + " pages and was sent to " 
      + " printer: " + HIGH_VOL_PRINTER + ".");

    // Record that the job was redirected in the application log.
    actions.log.info("Large job redirected from printer '" + inputs.job.printerName 
                     + "' to printer '" + HIGH_VOL_PRINTER + "'.");
  }

}

5 个答案:

答案 0 :(得分:3)

我相信这是您要寻找的东西,但尚不完全清楚。在不知道所有逻辑分支的情况下很难合并条件。

/*
* Redirect large jobs without confirmation
* 
* Users printing jobs larger than the defined number of pages have their jobs 
* automatically redirected to another printer or virtual queue.
* This can be used to redirect large jobs from slower or high cost printers 
* to more efficient or faster high volume printers.
*/

 function printJobHook(inputs, actions) {

 /*
 * This print hook will need access to all job details
 * so return if full job analysis is not yet complete.
 * The only job details that are available before analysis
 * are metadata such as username, printer name, and date.
 *
 * See reference documentation for full explanation.
 */

 /*
 * NOTE: The high-volume printer must be compatible with the source printer.
 *       i.e. use the same printer language like PCL or Postscript.
 *       If this is a virtual queue, all printers in the queue must use
 *       the same printer language.
 */


 var LIMIT             = 5; // Redirect jobs over 5 pages.
 var HIGH_VOL_PRINTER  = "Copier - Color";  

if (!inputs.job.isAnalysisComplete) { 
 return;// No job details yet so return. 
} 


//Charge jobs with less than 10 pages to non-bill account
if (inputs.job.totalPages < 10) {
  // Charge to the firm non-bill account
  actions.job.chargeToSharedAccount(ADM-3900);
} 
else //Charge jobs with more than 10 pages to the personal account
{   
    actions.job.chargeToPersonalAccount();

      if (inputs.job.totalPages > LIMIT) {
        /*
        * Specify actions.job.bypassReleaseQueue() if you wish to bypass the release queue
        * on the original printer the job was sent to.  (Otherwise if held at the target,
        * the job will need to be released from two different queues before it will print.)
        */
        actions.job.bypassReleaseQueue();

        /*
        * Job is larger than our page limit, so redirect to high-volume printer,
        * and send a message to the user.
        * Specify "allowHoldAtTarget":true to allow the job to be held at the hold/release
        * queue for the high-volume printer, if one is defined.
        */

        actions.job.redirect(HIGH_VOL_PRINTER, {allowHoldAtTarget: true});

        // Notify the user that the job was automatically redirected.
        actions.client.sendMessage(
          "The print job was over " + LIMIT + " pages and was sent to " 
          + " printer: " + HIGH_VOL_PRINTER + ".");

        // Record that the job was redirected in the application log.
        actions.log.info("Large job redirected from printer '" + inputs.job.printerName 
                         + "' to printer '" + HIGH_VOL_PRINTER + "'.");
      }
}
   return
}

答案 1 :(得分:0)

我认为您遇到的问题必须处理您提到的if语句块中的多个return语句。

这个街区就是你所拥有的...

if (!inputs.job.isAnalysisComplete) {
    return;
    actions.job.chargeToPersonalAccount();
    return;

    if (inputs.job.totalPages < 10) {
        actions.job.chargeToSharedAccount(ADM-3900);
    } 
} 

我认为,如果是这样的话,该区块会更准确...

/*No details of print analysis?  Return the the function immediately!*/
if (!inputs.job.isAnalysisComplete) {
    return;
} 

/*Job less than ten pages?  Charge shared account.  Otherwise charge personal account.*/
if (inputs.job.totalPages < 10) {

    /*Also, my bet is that the ADM-3900 needs to be in quotes for a string unless other wise stated in the manual.*/

    actions.job.chargeToSharedAccount("ADM-3900");
} else {
    actions.job.chargeToPersonalAccount();
}

注意,这是我最好的猜测,因为我对Papercut软件不熟悉。

如果那没有帮助,总会有technical support

答案 2 :(得分:0)

根据您对目标的理解,我将对您的代码进行下一步更改:

/*
* Redirect large jobs without confirmation
* 
* Users printing jobs larger than the defined number of pages have their jobs 
* automatically redirected to another printer or virtual queue.
* This can be used to redirect large jobs from slower or high cost printers 
* to more efficient or faster high volume printers.
*/

// Setup limit for charge the job to ADM-3900.

var ADM_3900_CHARGE_LIMIT = 10;

// Setup of redirection for larger jobs.

var REDIRECT_PAGE_LIMIT = 50;
var REDIRECT_PRINTER  = "Copier - Color";

function printJobHook(inputs, actions)
{    
    /*
    * This print hook will need access to all job details
    * so return if full job analysis is not yet complete.
    * The only job details that are available before analysis
    * are metadata such as username, printer name, and date.
    *
    * See reference documentation for full explanation.
    */

    /*
    * NOTE: The high-volume printer must be compatible with the source printer.
    *       i.e. use the same printer language like PCL or Postscript.
    *       If this is a virtual queue, all printers in the queue must use
    *       the same printer language.
    */

    // Check if job analysis is completed (return if not)

    if (!inputs.job.isAnalysisComplete)
    {
        // No job details yet so return.
        // XXX: We should return some value that the client can
        // identify and know he have to call the method again on a
        // few seconds (when job analysis is complete). the client could
        // also check this condition before calling us.
        return false;
    }

    // If pages to print is less than ADM_3900_CHARGE_LIMIT,
    // just charge the job to the firm non-billable (ADM-3900) account.

    if (inputs.job.totalPages < ADM_3900_CHARGE_LIMIT)
    {
        // Charge to the firm non-bill account.
        actions.job.chargeToSharedAccount(ADM-3900);

        // Return with success.
        return true;
    }

    // At this point, we have to charge to personal account.

    actions.job.chargeToPersonalAccount();

    // Finally, check if we have to redirect to a more efficient or
    // faster high volume printer.

    if (inputs.job.totalPages > REDIRECT_PAGE_LIMIT)
    {
        /*
        * Specify actions.job.bypassReleaseQueue() if you wish to bypass
        * the release queue on the original printer the job was sent to.
        * (Otherwise if held at the target, the job will need to be released
        * from two different queues before it will print.)
        */

        actions.job.bypassReleaseQueue();

        /*
        * Job is larger than our page limit, so redirect to high-volume printer,
        * and send a message to the user.
        * Specify "allowHoldAtTarget":true to allow the job to be held at the
        * hold/release queue for the high-volume printer, if one is defined.
        */

        actions.job.redirect(REDIRECT_PRINTER, {allowHoldAtTarget: true});

        // Notify the user that the job was automatically redirected.

        actions.client.sendMessage(
            "The print job was over " + REDIRECT_PAGE_LIMIT + " pages and" +
            " was sent to printer: " + REDIRECT_PRINTER + "."
        );

        // Record that the job was redirected in the application log.

        actions.log.info(
            "Large job redirected from printer '" + inputs.job.printerName +
            "' to printer '" + REDIRECT_PRINTER + "'."
        );
    }

    // Return with success.
    return true;
}

答案 3 :(得分:0)

我发送的代码,以达到指定的目标

/*
* Redirect large jobs without confirmation
* 
* Users printing jobs larger than the defined number of pages have their jobs 
* automatically redirected to another printer or virtual queue.
* This can be used to redirect large jobs from slower or high cost printers 
* to more efficient or faster high volume printers.
*/

function printJobHook(inputs, actions) {

  /*
  * This print hook will need access to all job details
  * so return if full job analysis is not yet complete.
  * The only job details that are available before analysis
  * are metadata such as username, printer name, and date.
  *
  * See reference documentation for full explanation.
  */
  if (!inputs.job.isAnalysisComplete) {
    // No job details yet so return.
    return;
  }
  /*
  * NOTE: The high-volume printer must be compatible with the source printer.
  *       i.e. use the same printer language like PCL or Postscript.
  *       If this is a virtual queue, all printers in the queue must use
  *       the same printer language.
  */

  if (inputs.job.totalPages < 10) {
    // Below 10 - charge to the firm non-bill account
    actions.job.chargeToSharedAccount("ADM-3900");
  }
  else{
    //job is 10+ pages - do the cost center popup

    actions.job.chargeToPersonalAccount();
    // Account Selection will still show

    var LIMIT             = 50; // Redirect jobs over 50+ pages.
    var HIGH_VOL_PRINTER  = "Copier - Color";

    if (inputs.job.totalPages > LIMIT) {
      //The job is 50+ pages

      /*
      * Specify actions.job.bypassReleaseQueue() if you wish to bypass the release queue
      * on the original printer the job was sent to.  (Otherwise if held at the target,
      * the job will need to be released from two different queues before it will print.)
      */
      actions.job.bypassReleaseQueue();

      /*
      * Job is larger than our page limit, so redirect to high-volume printer,
      * and send a message to the user.
      * Specify "allowHoldAtTarget":true to allow the job to be held at the hold/release
      * queue for the high-volume printer, if one is defined.
      */
      actions.job.redirect(HIGH_VOL_PRINTER, {allowHoldAtTarget: true});

      // Notify the user that the job was automatically redirected.
      actions.client.sendMessage(
        "The print job was over " + LIMIT + " pages and was sent to " 
        + " printer: " + HIGH_VOL_PRINTER + ".");

      // Record that the job was redirected in the application log.
      actions.log.info("Large job redirected from printer '" + inputs.job.printerName 
                     + "' to printer '" + HIGH_VOL_PRINTER + "'.");
    }
  }
}

答案 4 :(得分:0)

您的文字翻译为:

if(jobs > 50) redirect to Copier-Color
elseif(jobs > 10) cost center popup
else charge automagically

在JS中应该是:

const   LIMIT_10            = 10,
        LIMIT_50            = 50,
        HIGH_VOL_PRINTER  = "Copier - Color";

function printJobHook(inputs, actions) {
    if (!inputs.job.isAnalysisComplete)
        return;

    if (inputs.job.totalPages > LIMIT_50) {
        actions.job.bypassReleaseQueue();
        actions.job.redirect(HIGH_VOL_PRINTER, {allowHoldAtTarget: true});
        actions.client.sendMessage(
          "The print job was over " + LIMIT + " pages and was sent to " 
          + " printer: " + HIGH_VOL_PRINTER + ".");

        actions.log.info("Large job redirected from printer '" + inputs.job.printerName 
                         + "' to printer '" + HIGH_VOL_PRINTER + "'.");
    }else if (inputs.job.totalPages > LIMIT_10) {
        actions.job.chargeToSharedAccount();
    }else{
        actions.job.chargeToPersonalAccount(ADM-3900);
    }
}