首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将<ul>拆分为<table>和<ul>

将<ul>拆分为<table>和<ul>
EN

Stack Overflow用户
提问于 2022-07-06 12:06:58
回答 2查看 57关注 0票数 -1

这是我们的产品的详细信息,在我们将其导入到Shopify以显示在我们的e-com商店中之前,我们的产品的外观和格式如下:

代码语言:javascript
复制
<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>

我想要实现的是将^转化为:

代码语言:javascript
复制
<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>被转换为

代码语言:javascript
复制
<tr>
    <td>Material:</td>
    <td>100% polyester with polyurethane coating</td>
  </tr>

这是否可以在谷歌单张中自动完成,其中每个单元格都包含一个产品的详细信息。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2022-07-06 14:49:31

描述

使用App,下面的示例脚本将接受每一行,并将包含:的任何行转换为表。此and的最终结果是由所有和行组成的单个字符串。

Code.gs

代码语言:javascript
复制
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);
  }
}

执行日志

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2022-07-07 01:10:01

试一试

代码语言:javascript
复制
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>'))
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72883297

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档