使用Groovy将0添加到空的csv单元格 [英] Add 0 to empty csv cells using Groovy

查看:179
本文介绍了使用Groovy将0添加到空的csv单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用groovy脚本连接CSV文件上的2个字段。
脚本运行正常,除非它碰到空单元格时会崩溃。

Im using a groovy script to concatenate between 2 fields on a CSV files. The script is working perfectly except it collapse once it hits empty cells.

我设法使用powershell脚本来避免这个东西,编码问题

I managed to use a powershell script to avoid this thing but then i got stuck with encoding issue

    <groovy>
  ant.mkdir(dir:"target")


  new File("target/UpsertCheckDeals.csv").withWriter {
    new File("C:/Users/alon\Documents\CheckDealBeforeUpsert.csv").splitEachLine(",") { Customer__c, Name__c, Deal__c, Amount__c, CheckCount__c, Discount__c ->
       it.println "${Customer__c},${Name__c},${CheckCount__c},${Deal__c},${Amount__c},${Discount__c},${Customer__c}-${Deal__c }"
    }
  }

</groovy>

有任何建议如何使用groovy解决这个问题?

Any suggestions how to solve this with groovy?

提前感谢

推荐答案

没有错误的详细信息,我将假设 splitEachLine 方法无法将该行拆分为足够的部分,以便为所有Closure声明的参数分配值。

Without details on the error, I'm going to assume the splitEachLine method cannot split the line into enough parts in order to assign values to all of the Closure's declared arguments.

为了解决这个问题,我将使用一个单一的闭包。单个参数被分配一个分割值的数组(List?)。您可以在尝试处理该行之前检查数组的长度。

To workaround this, I would use a single-arg closure. The single argument is assigned an array (List?) of the split values. You can check the length of the array before you attempt to process the row.

new File("target/UpsertCheckDeals.csv").withWriter {
    new File("C:/Users/alon\Documents\CheckDealBeforeUpsert.csv").splitEachLine(",") { parts ->
        if (parts.length != 6) return // or maybe it's .size()
        (Customer__c, Name__c, Deal__c, Amount__c, CheckCount__c, Discount__c) = parts
        it.println "${Customer__c},${Name__c},${CheckCount__c},${Deal__c},${Amount__c},${Discount__c},${Customer__c}-${Deal__c }"
    }
  }

这篇关于使用Groovy将0添加到空的csv单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆