在javascript中删除行首的逗号
Removing commas at the beginning of a line in javascript
我正在尝试从动态生成的表创建CSV文件。我有一个代码,生成一个合理的CSV。代码的输出是
, Emp I D, Emp Name, Emp Title,
, 111 , andro1 , developer ,
, 112 , andro2 , developer ,
, 113 , andro3 , developer ,
我想删除每行开头的逗号,以便csv格式良好。我还想整齐地格式化这个CSV,以便它在excel中正确打开(标题粗体,间距良好的列等)。
我正在粘贴我使用的示例javascript
//Modify the value of these variable for your coach
var tableIdsToExport = "Table0";
// a comma delimited list of the ids of the tables that you want to include in the excel export
// if you include a tableId that does not exist in your coach, you will recieve a client side error message
//You do not need to modify the following part of the script
var form = document.createElement('div');
form.innerHTML = '<form name="exportData" method="post" target="_new" action="data-redirect.jsp"><input type="hidden" name="contentType" value="text/csv"><input type="hidden" name="data" value=""><input type="hidden" name="fileName" value="filename=reportData.csv"></form>';
//Work around a bug in IE: http://support.microsoft.com/default.aspx/kb/927917
document.getElementsByTagName("H1")[0].appendChild(form);
//document.body.appendChild(form);
function addExportData(csvTable) {
if (document.forms.exportData == null || document.forms.exportData.data == null) {
return;
}
document.forms.exportData.data.value = document.forms.exportData.data.value +
"'n'n" + csvTable;
}
function doSubmitExport() {
var tableArr = tableIdsToExport.split(",");
for (var i=0;i<tableArr.length;i++) {
addTableToCSV(tableArr[i]);
alert(addTableToCSV(tableArr[i]));
}
document.forms["exportData"].submit();
}
function addTableToCSV(tableId) {
var table;
try {
table = document.getElementById(tableId);
var a = table.innerHTML;
//replace existing commas with semi-colons. Poor mans way of handling embedded commas in a csv file
a = a.replace(/,/g, ";");
//get rid of javascript blocks
a = a.replace(/<script(.|'n)*?<'/script>/gi, "");
//insert commas at the end of a table cell
a = a.replace(/<'/td>/g, ",");
a = a.replace(/<'/TD>/g, ",");
a = a.replace(/<'/th>/g, ",");
a = a.replace(/<'/TH>/g, ",");
//insert a newline tag at the end of every row. Need to do this before removing all tags
a = a.replace(/<'/tr>/g, "---newline---");
a = a.replace(/<'/TR>/g, "---newline---");
//remove html tags
a = a.replace(/<'/?[^>]+(>|$)/g, "");
//remove whitespace (regexs found via google)
a = a.replace(/'r/g, " ");
a = a.replace(/[^ A-Za-z0-9`~!@#'$%'^&'*'(')-_='+'''|']'['}'{'";:'?'/'.>,<]/g, "");
a = a.replace(/'/g, "");
a = a.replace(/ +/g, " ");
a = a.replace(/^'s/g, "");
a = a.replace(/'s$/g, "");
//put newlines in
a = a.replace(/---newline---/g, "'n");
//replace   which the coach designer inserts
a = a.replace(/' /g, " ");
//a now holds a resonable csv that I can put in excel
//alert(a);
addExportData(a);
return true;
} catch (e) {
alert("Table Export Error: " + e);
}
return true;
}
</script>
还有一件事,在表中有一列是空的。这就是脚本在开头返回一个逗号的原因,我想删除它。当我尝试在excel中打开csv时,内容从第三行开始,而不是第一行。
这是在IBM BPM Lombardi应用程序中使用的,我试图导出一个动态生成的表到excel。
我也想知道如何通过jquery实现相同的结果提前感谢
一旦完成,我还计划导出为pdf选项。
下面解释了在脚本
中所做的事情执行导出的关键组件是包含客户端JavaScript的自定义HTML代码块。JavaScript包含以下组件:
1。一个JavaScript变量tableIdsToExport。这是一个逗号分隔的字符串,列出要导出的表元素id。您必须确保这些id与您的Coach中的id匹配。
2。一个JavaScript变量表单,它是动态生成的HTML表单元素(不可显示)。此表单的动作属性的URI是一个Teamworks JSP,它将返回电子表格。该表单还包含一个输入字段data,其中包含要导出的表。
3。"导出到excel"按钮。当这个按钮被选中时,doSubmitExport函数被执行。
4。doSubmitExport函数。这个函数的算法执行两个任务。首先,它遍历tableIdsToExport中的每个表,并调用addTableToCSV函数。迭代完成后,该函数提交表单变量(如上所述)中包含的表单元素。
5。addTableToCSV函数。对于tableIdsToExport列表中的每个表调用这个函数,它有两个基本任务要执行。首先,对HTML表进行转换,以便将表元素替换为逗号和换行符,从而有效地将表转换为CSV。接下来,调用addTableToExport函数,将转换后的表作为参数传递。
6。addTableToExport函数。该函数接受一个CSV参数,并将其附加到隐藏表单的数据字段。提交后,表单被发布到Teamworks data-redirect.jsp,后者应用适当的内容类型并将数据发送回浏览器。
我同意David Thomas的观点,他说你应该修复CSV生成脚本中的问题。
但是如果你不能控制这个脚本,你可以在javascript replace()
中使用正则表达式将逗号的第一个出现替换为空字符串(删除它):
", 111 , andro1 , developer ,".replace(/^,/, '');
返回:
" 111 , andro1 , developer ,"
更新:
在这里您可以看到一个javascript的演示,它删除了第一个逗号,并将所有列对齐到预定义的最大宽度。
因为你没有jQuery标签,我假设你想要纯Javascript,但请记住,使用jQuery会容易得多。
您可以从演示中看到的最终输出是 EmpID, EmpName, EmpTitle,
111, andro1, developer,
112, andro2, developer,
113, andro3, developer,
在这种情况下,我正确地将每列对齐为10个字符。
关于列标题中的粗体,这在CSV格式中是不可能的,因为CSV是纯文本格式。您可以考虑这个xlwt。
再一次,我同意你应该从生成脚本中解决这个格式化问题,但如果你只是这些文件的消费者,并希望在javascript中修复它们,那么这个解决方案应该适合你。
更新2 您需要清理该javascript。请阅读我上面包含的regexp链接。
你可以替换这个:
a = a.replace(/<'/td>/g, ",");
a = a.replace(/<'/TD>/g, ",");
a = a.replace(/<'/th>/g, ",");
a = a.replace(/<'/TH>/g, ",");
:
a = a.replace(/<'/t(d|h)>/gi, '');
为什么用--newline--
代替</tr>
和</TR>
,然后又用'n
代替--newline--
?您可以简单地在一个调用中完成此操作:
a = a.replace(/<'/tr>/gi, '''n');
所以,我认为你最终有一个单一的字符串与所有csv内容,其中每行由'n
分隔。
这里是处理这个用例的更新脚本。我只更改了var
声明,核心保持不变。
对于格式化表达式,正则表达式将无法帮助您。这种格式化必须在CSV生成脚本中完成。
- 手持:删除行后更新渲染器
- 在表中找到元素时删除行
- 无法使用jQuery从表中删除行
- 向具有唯一Id的表添加/删除行
- 当行增加时自动递增序列号,并在 jQuery 中删除行时自动重新调整编号顺序
- 使用 for 循环基于
id 从表中删除行 - 无法从表中删除行
- 使用 Javascript 动态删除行.我的代码有什么问题
- 删除行中多余元素的函数
- 使用php中的单选按钮从MS SQL SERVER中删除行
- 按类名从表中删除行
- 类型错误:无法读取属性'行'使用javascript删除行时为null
- 正在从表行中删除本地存储数据
- 单击a href删除行(TR)
- NetSuite行项目删除内部循环问题
- 使用ajax从数据库Mvc中的表中删除行
- 根据活动单元格删除行
- 使用jQuery删除行
- 删除行后删除表
- 在javascript中删除行首的逗号