我正在尝试在单元格中对HTML块进行编码,以使HTML可以显示在用<pre>
标签包裹的网页上。
所以
<div class="html">
<html>
<head>
<title>Title</title>
</head>
<body>
<p>Unrendred html</p> </body>
</html>
</div>
成为
<pre>
<div class="html">
<html>
<head><title>Title</title></head>
<body>
<p>Unrendred html</p>
</body>
</html>
</div>
</pre>
现在,我以为我已经找到了=ENCODEURL
公式,但是输出结果略有不同。
%26lt%3Bdiv%20class%3D%26quot%3Bhtml%26quot%3B%26gt%3B%20%26lt%3Bhtml%26gt%3B%20%26lt%3Bhead%26gt%3B%20%26lt%3Btitle%26gt%3BTitle%26lt%3B%2Ftitle%26gt%3B%20%26lt%3B%2Fhead%26gt%3B%20%26lt%3Bbody%26gt%3B%20%26lt%3Bp%26gt%3BUnrendred%20html%26lt%3B%2Fp%26gt%3B%20%26lt%3B%2Fbody%26gt%3B%20%26lt%3B%2Fhtml%26gt%3B%20%26lt%3B%2Fdiv%26gt%3B
此CodePen显示了理想的输出以及如何在HTML中成功显示HTML代码。
我认为“编码”选项称为HTML特殊字符。如何在Google表格中获取此信息?也许通过公式或正则表达式
答案 0 :(得分:1)
我正在通过JSON访问我的Google Spread表格单元格,并在其中添加了HTML。
Google Spread Sheet: https://docs.google.com/spreadsheets/d/17zfaoB56EfdSc9IzkJitSRi5wLGv-GJQwZaOPj5cZM0/edit#gid=0
Google Spread Sheet JSON: https://spreadsheets.google.com/feeds/cells/17zfaoB56EfdSc9IzkJitSRi5wLGv-GJQwZaOPj5cZM0/1/public/full?alt=json
Codepen演示:https://codepen.io/aystarz52/pen/ormamN?editors=1011
$(function(){
var sheetUrl = 'https://spreadsheets.google.com/feeds/cells/17zfaoB56EfdSc9IzkJitSRi5wLGv-GJQwZaOPj5cZM0/1/public/full?alt=json';
$.getJSON(sheetUrl, function(data){
var entry = data.feed.entry;
var htmlCode = []; // the leftmost column of the Google Sheets
for (var i = 0; i < entry.length; i += 1){
// entry[i].content.$t retrieves the content of each cell
htmlCode.push(entry[i].content.$t);
}
var uri= escape_html(htmlCode);
var res = escape(uri);
var appendContents = document.getElementById("demo").textContent = uri;
})
});
function escape_html(str) {
if ((str===null) || (str===''))
return false;
else
str = str.toString();
var map = {
'<': '<',
'>': '>',
'"': '"',
"'": ''',
";": ';'
};
return str.replace(/[<>"']/g, function(m) { return map[m]; });
}