这是我们的产品的详细信息,在我们将其导入到Shopify以显示在我们的e-com商店中之前,我们的产品的外观和格式如下:
<ul>
<li>Comfort: waterproof, windproof, lightweight, engineered ventilation</li>
<li>Material: 100% polyester with polyurethane coating</li>
<li>Water column pressure: 4000mm</li>
<li>Fit: Casual unisex</li>
<li>Snap closure</li>
<li>Placket with snap fastenings</li>
<li>Drawstring hood with cap brim</li>
<li>Snap adjustable cuffs</li>
<li>Back yoke with concealed vents</li>
<li>Two side pockets with snaps</li>
<li>Eyelets at arm holes</li>
<li>Ultrasonically welded seams</li>
<li>Fishtail hem</li>
</ul>我想要实现的是将^转化为:
<table>
<tr>
<td>Comfort:</td>
<td>waterproof, windproof, lightweight, engineered ventilation</td>
</tr>
<tr>
<td>Material:</td>
<td>100% polyester with polyurethane coating</td>
</tr>
<tr>
<td>Water column pressure:</td>
<td>4000mm</td>
</tr>
<tr>
<td>Fit:</td>
<td>Casual unisex</td>
</tr>
</table>
<ul>
<li>Snap closure</li>
<li>Placket with snap fastenings</li>
<li>Drawstring hood with cap brim</li>
<li>Snap adjustable cuffs</li>
<li>Back yoke with concealed vents</li>
<li>Two side pockets with snaps</li>
<li>Eyelets at arm holes</li>
<li>Ultrasonically welded seams</li>
<li>Fishtail hem</li>
</ul>其背后的逻辑是:所有具有冒号的行都应该是表格式的。其余的应该是列表格式的.。
如果该行有冒号,则应将其拆分为表中的两个不同的列。例如,<li>Material: 100% polyester with polyurethane coating</li>被转换为
<tr>
<td>Material:</td>
<td>100% polyester with polyurethane coating</td>
</tr>这是否可以在谷歌单张中自动完成,其中每个单元格都包含一个产品的详细信息。
谢谢!
发布于 2022-07-06 14:49:31
描述
使用App,下面的示例脚本将接受每一行,并将包含:的任何行转换为表。此and的最终结果是由所有和行组成的单个字符串。
Code.gs
function test() {
try {
let data = `<ul>
<li>Comfort: waterproof, windproof, lightweight, engineered ventilation</li>
<li>Material: 100% polyester with polyurethane coating</li>
<li>Water column pressure: 4000mm</li>
<li>Fit: Casual unisex</li>
<li>Snap closure</li>
<li>Placket with snap fastenings</li>
<li>Drawstring hood with cap brim</li>
<li>Snap adjustable cuffs</li>
<li>Back yoke with concealed vents</li>
<li>Two side pockets with snaps</li>
<li>Eyelets at arm holes</li>
<li>Ultrasonically welded seams</li>
<li>Fishtail hem</li>
</ul>`;
// asuming every line is seperated by a \n new line
data = data.split('\n');
let table = [];
for( let i=0; i<data.length; i++ ) {
// remove old tags
if( ( data[i] === '<ul>' ) || ( data[i] === '</ul>' ) ) continue;
data[i] = data[i].replace("<li>","");
data[i] = data[i].replace("</li>","");
// now get lines that are part of a table
table.push(data[i].split(":"));
}
// now add the new tags
let results = [];
let newTable = false;
let newUl = false;
for( let i=0; i<table.length; i++ ) {
if( table[i].length > 1 ) {
if( newUl ) {
// close out unordered list
results.push(["</ul>"]);
newUl = false;
}
if( !newTable ) {
// create new table
newTable = true;
results.push(['<table>']);
}
// add a row
results.push(["<td>"+table[i][0]+"</td><td>"+table[i][1].trim()+"</td>"]);
}
else {
if( newTable ) {
// close out old table
results.push(["</table>"]);
newTable = false;
}
if( !newUl ) {
// create new unordered list
newUl = true;
results.push(["<ul>"]);
}
// add a list item
results.push(["<li>"+table[i][0]+"</li>"]);
}
}
if( newTable ) results.push(["</table>"]);
if( newUl ) results.push(["</ul>"]);
results = results.join(" ");
console.log(results);
}
catch(err) {
console.log(err);
}
}执行日志
7:21:43 AM Notice Execution started
7:21:46 AM Info <table> <td>Comfort</td><td>waterproof, windproof, lightweight, engineered ventilation</td> <td>Material</td><td>100% polyester with polyurethane coating</td> <td>Water column pressure</td><td>4000mm</td> <td>Fit</td><td>Casual unisex</td> </table> <ul> <li>Snap closure</li> <li>Placket with snap fastenings</li> <li>Drawstring hood with cap brim</li> <li>Snap adjustable cuffs</li> <li>Back yoke with concealed vents</li> <li>Two side pockets with snaps</li> <li>Eyelets at arm holes</li> <li>Ultrasonically welded seams</li> <li>Fishtail hem</li> </ul>
7:21:44 AM Notice Execution completed发布于 2022-07-07 01:10:01
试一试
function transform(input) {
return (input.replace(/<\/ul>/g, '</table>')
.replace(/<ul>/g, '<table>')
.replace(/<\/li>/g, '</td></tr>')
.replace(/<li>/g, '<tr><td>')
.replace(/:/g, '</td><td>'))
}

https://stackoverflow.com/questions/72883297
复制相似问题