拆分< ul>进入a< table>和a< ul>

发布于 2025-02-13 22:13:19 字数 2096 浏览 3 评论 0原文

这是我们的一种产品的产品详细信息,并且在将其导入Shopify之前要在我们的电子杂志商店中显示:

<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>

它背后的逻辑:所有带有结肠的线应处于表格格式。其余的应为列表格式。

当线带有结肠时,应将其分为表格中的两个不同的列。例如,&lt; li&gt;材料:带有聚氨酯涂层的100%聚酯蛋白&lt;/li&gt;

<tr>
    <td>Material:</td>
    <td>100% polyester with polyurethane coating</td>
  </tr>

可以在Google表中自动完成,其中每个单元格都包含一个产品的产品详细信息。

谢谢!

This is what the product details for one of our products look like and is formatted before we import it into Shopify to be shown on our e-com store:

<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>

What I would like to achieve is to convert that ^ into this:

<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>

The logic behind it: All lines that has a colon should be in a table format. The rest should be in a list format.

When the line has a colon, it should be split into two different columns inside the table. For example, <li>Material: 100% polyester with polyurethane coating</li> is turned into

<tr>
    <td>Material:</td>
    <td>100% polyester with polyurethane coating</td>
  </tr>

Can this be automatically done in Google Sheets where each cell contain product details for one product.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两相知 2025-02-20 22:13:20

尝试

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>'))
}

Try

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>'))
}

enter image description here

蓝色星空 2025-02-20 22:13:20

Description

使用应用脚本以下示例脚本将每行并将包含的所有行转换为表。此scipt的最终结果是所有和行的单字符串

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

Description

Using App script the following example script will take each row and convert any rows that contain : into a table. The end result of this scipt is a single string of all the and lines

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);
  }
}

Execution log

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文