简体   繁体   English

将csv文件加载到javascript文件中

[英]load csv file into a javascript file

Is this possible ? 这可能吗 ? I am wanting to pull out a price of a product from the csv file and tie that price to its particular part number. 我想从csv文件中提取某种产品的价格,并将该价格绑定到其特定的部件号。

I am doing this for a website and I am learning script programming at the same time, and I am unsure how to do this. 我正在为网站执行此操作,同时正在学习脚本编程,但是我不确定如何执行此操作。

The JS file would call the csv file and pull the price and part number out of it. JS文件将调用csv文件,并从中提取价格和部件号。

From there the JS file will create a form to use with a Paypal shopping cart. 从那里JS文件将创建一个与Paypal购物车一起使用的表单。

This is the paypal shopping cart code for the add to cart button: 这是“添加到购物车”按钮的贝宝购物车代码:

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="business" value="XXXXX">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="PartNumber1">
<input type="hidden" name="amount" >
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="products">
<input type="hidden" name="cn" value="Add special instructions to the seller">
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="weight" value=".5">
<input type="hidden" name="weight_unit" value="lbs">
<input type="hidden" name="add" value="1">
<input type="hidden" name="bn" value="PP-ShopCartBF:btn_cart_LG.gif:NonHosted">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

Using the variable I added to this button, the JS file would look similar to this: 使用我添加到此按钮的变量,JS文件将类似于以下内容:

function CalculateOrder(form)
{

if (form.item_name.value == "PartNumber1"
{
form.amount.value = (The price variable would go here);
}

if (form.item_name.value == "PartNumber2"
{
form.amount.value = (The price variable would go here);
}
}

What I need before this code in the JS file is the code that loads the csv file and outputs the price and part number. 在JS文件中此代码之前,我需要的是加载csv文件并输出价格和部件号的代码。 The JS file would search for the part number labelled called "PartNumber1" and output the appropriate price from the csv file. JS文件将搜索标记为“ PartNumber1”的零件号,并从csv文件中输出适当的价格。

HELP PLEASE !! 请帮助 !!

It is possible, but quite possibly more trouble then it is worth. 这是可能的,但很可能会带来更多的麻烦。

First you have to get the CSV data. 首先,您必须获取CSV数据。 This is easy, so long as it resides on the same server as the site - XMLHttpRequest is your friend. 只要它与站点位于同一服务器上,这就很容易-XMLHttpRequest是您的朋友。 You can get the raw CSV out of of the responseText. 您可以从responseText中获取原始CSV。

Then you need to parse it. 然后,您需要解析它。

If you have a very simple CSV file, then you can split first on new lines (to get each row) and then on commas (to get each column). 如果您有一个非常简单的CSV文件,则可以先在新行上分割(以获取每一行),然后在逗号上分割(以获取每一列)。

Without testing, something like this might do the trick: 如果不进行测试,则可能会发生以下情况:

var csv = myXHR.responseText;
var rows = csv.split("\n");
for (var i = 0; i < rows.length; i++) {
  rows[i] = rows[i].split(",");
}

You could then look for the part number you want with: 然后,您可以查找所需的零件号:

var getPart = function getPart(partNumber) {
  for (var i = 0, j < rows.length; i < j; i++) {
    if (row[i][partNumberColumnIndex] === partNumber) {
      return row[i];
    } 
  }
};

However, CSV isn't as simple a data format as it looks at first . 但是, CSV并不像一开始那样简单

If any of the data has a comma or a new line in it, then you can't simply split on those characters since you have to watch out for quote characters. 如果任何数据中包含逗号或换行符,那么您就不能简单地分割这些字符,因为您必须注意引号字符。

If I had CSV data that I really wanted to deal with on the client, then I would use Text::CSV and JSON::Any to convert to a format that JavaScript can parse natively before sending it to the client. 如果我确实要在客户端上处理CSV数据,则可以使用Text :: CSVJSON :: Any转换为JavaScript可以在将其发送到客户端之前本地解析的格式。

For this particular task, I'd probably forget about doing it client side altogether and just do everything on the server. 对于此特定任务,我可能会完全忘记在客户端执行此操作,而只是在服务器上执行所有操作。 I can't see much benefit on pushing this work off to the client. 我看不到将这项工作推向客户的好处。 (Although, admittedly, I don't know all the details). (尽管,诚然,我不知道所有细节)。

Sometime ago I wrote a script which downloads a CSV and plots the points . 不久前,我编写了一个脚本,脚本下载CSV并绘制点 Perhaps it's a good starting point for you. 也许这对您来说是一个很好的起点。

如果您需要一个支持CSV RFC棘手部分的JavaScript CSV解析器,则可能需要看一下我对解析器的实现 -但是请注意,这只是第一个版本,仍然需要清理一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM