简体   繁体   English

将CSV从实时网址解析为HTML表

[英]Parse CSV from live url to HTML Table

Where can I get an example of a simple way to retrieve a CSV file from a URL and put the contents into my HTML body? 在哪里可以找到从URL检索CSV文件并将内容放入HTML正文的简单方法的示例? Just a simple JavaScript or jquery to automatically pull the latest CSV contents online and run it through my html script and put table tags around each line of the CSV. 只需一个简单的JavaScript或jquery,即可自动在线提取最新的CSV内容,并通过我的html脚本运行它,并将表格标签放在CSV的每一行周围。 That way when the CSV file is published, for example google sheets CSV output, the website will show the table with the updated contents. 这样,当发布CSV文件(例如Google表格CSV输出)时,网站将显示带有更新内容的表格。

I have tried looking at tutorials online but most revolve around converting a physical file or uploading a local path but nothing from a public URL. 我尝试过在线查看教程,但大多数都围绕转换物理文件或上传本地路径进行,但都没有公开URL。

If this is not a fair question just tell me before down voting me so I can delete it if need be. 如果这不是一个公平的问题,请在拒绝我之前告诉我,这样我就可以删除它。

You don't need to change anything if you want to look at a public file - you just do this with jQuery: 如果要查看公共文件,则无需更改任何内容-只需使用jQuery即可:

$.ajax({
    url: "https://upload.cat/e307f4f114696856",
    dataType: "text"
}).done(function(data) {
    $("body").append(data);
});

Then you can do whatever you want with this line: 然后,您可以使用此行执行任何操作:

$("body").append(data);

To make the data look good on your page. 为了使数据在页面上看起来不错。

Here is example of using google sheet to draw html 这是使用Google表格绘制HTML的示例

You get your data with ajax. 您可以使用ajax获取数据。 In this case axios and then parse it. 在这种情况下,axios然后对其进行解析。

var app = new Vue({
el: '#app',
mounted() {
    let vm = this
    axios
        .get(
            'https://sheets.googleapis.com/v4/spreadsheets/1zIVCVA0Tk5CvAiTyeAdDBPygT3aKDiSeM2FbPU0JO2c/values/Specials!A2:D20?key=AIzaSyBhiqVypmyLHYPmqZYtvdSvxEopcLZBdYU'
        )
        .then(function (response) {
            let specials = response.data.values
            for (let index = 0; index < specials.length; index++) {
                const element = specials[index]
                let mitem = {
                    name: element[0],
                    description: element[1],
                    price: element[2]
                }
                if (vm.isEven(index)) {
                    vm.menuItems_L = vm.menuItems_L.concat(mitem)
                } else {
                    vm.menuItems_R = vm.menuItems_R.concat(mitem)
                }
            }
            console.log(response)
        })
},
data: {
    menuItems_L: [],
    menuItems_R: [],
    menuStyle: {
        background: '#ffe6d1',
        color: '#000'
    },
    dotStyle: {
        backgroundImage: 'radial-gradient(' + this.color + ' 1px, transparent 0px)'
    }
},
computed: {},
methods: {
    isEven: function (n) {
        return n % 2 == 0
    }
}

}); });

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

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