這里有一個簡單的問題:我在 Javascript 中有以下資料子集。我的應用程式可以作業,但我需要一種簡單的方法來引入相同資料的擴展版本,但從 Excel 檔案中獲取,而不是手動輸入到此腳本中。什么是最簡單和最簡單的解決方案,中間只需幾個步驟?
const countryData={
australia: {
countryName: "France",
fsnNarrative: "aus fsn lorem ipsum dolor sit amet",
otherNarrative: "lorem ipsum dolor",
marketLink: "http://www.google.com",
center: [65, 34],
zoom:4
},
france: {
countryName: "France",
fsnNarrative: "bgd fsn lorem ipsum dolor sit amet",
otherNarrative: "lorem ipsum dolor",
marketLink: "http://www.youtube.com",
center: [90, 24],
zoom: 5
}
}
我嘗試將它從 JSON 轉換,但也遇到了麻煩 a) 將 excel 轉換為 JSON 和 b) 將外部本地 JSON 分配給常量(嘗試遵循此指南但不清楚。我也使用 R,以防有人有一個解決方案。
最理想的情況是,我可以從 Javascript 鏈接到外部 JSON 或其他檔案型別,就像這樣。const data = (path to some URL)
目前只使用 Javascript、HTML 和 CSS。
uj5u.com熱心網友回復:
我使用SheetJS庫來處理和匯出 XLS 資料。請檢查下面的片段
var json_data;
(async() => {
// fetch xls file
const url = "https://www.lucasroquilly.com/stack_overflow_samples/workbook.xls";
// store data
const data = await (await fetch(url)).arrayBuffer();
// convert to workbook
const workbook = XLSX.read(data);
//get first sheet
const worksheet = workbook.Sheets[workbook.SheetNames[0]]
//convert sheet to json
json_data = XLSX.utils.sheet_to_json(worksheet);
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.min.js"></script>
<button onclick="console.log(json_data)">Log json</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/486473.html
標籤:javascript html r json
上一篇:沒有在jquery和laravel中動態添加輸入文本的post值
下一篇:按反向Java排序的自定義排序