我正在尝试从 Google 电子表格中获取单元格文本,并使用此代码将其插入到 WordPress 上的帖子中,但我对 JavaScript 的了解很差,我什至不知道如何在没有警报的情况下获取文本.
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> </script>
</head>
<body>
<script>
$.ajax("https://docs.google.com/spreadsheets/d/e/2PACX-1vQhp9yFq8eXagN03gn-mCN3_KPWRc2EIpswDFpHJLflFOG-XU2OMktqj03gxvUBZMAp8gYwWO5Q3MVJ/pub?gid=942917560&single=true&range=d3&output=csv").done(function(result){
alert(result);
});
</script>
</body>
最佳答案
在您的 WordPress 帖子中,转到文本模式并添加 div
<div id='spreadsheet'></div>
现在在您的脚本中执行以下操作来设置内容:
jQuery.ajax("https://docs.google.com/spreadsheets/d/e/2PACX-1vQhp9yFq8eXagN03gn-mCN3_KPWRc2EIpswDFpHJLflFOG-XU2OMktqj03gxvUBZMAp8gYwWO5Q3MVJ/pub?gid=942917560&single=true&range=d3&output=csv").done(function(result){
jQuery("#spreadsheet").text(result);
})
WordPress通常自带jQuery,你不需要自己添加。不过,您需要将 $
替换为 jQuery
。
您还可以这样做:
jQuery(function($) {
$.ajax("https://docs.google.com/spreadsheets/d/e/2PACX-1vQhp9yFq8eXagN03gn-mCN3_KPWRc2EIpswDFpHJLflFOG-XU2OMktqj03gxvUBZMAp8gYwWO5Q3MVJ/pub?gid=942917560&single=true&range=d3&output=csv").done(function(result) {
$("#spreadsheet").text(result);
})
});
这将 a) 仅在加载文档后运行该函数,b) 允许您在代码中继续使用 $
。
关于javascript - 将数据从 Google 电子表格单元导入到 WordPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46081625/