ล้อมเนื้อหา Google doc ด้วย divs และเปลี่ยนเป็นไฟล์ html

ฉันกำลังพยายามเปลี่ยนไฟล์ Google docs เป็นไฟล์ html ฉันให้แท็ก html แก่เนื้อหาผ่าน Google เอกสาร (h1, h2, p ฯลฯ) จากนั้นฉันดาวน์โหลดเป็นไฟล์ HTML และใช้งานได้ดี

ฉันมีปัญหาอย่างหนึ่ง - ฉันต้องการล้อมเนื้อหาเฉพาะในไฟล์ Google Doc ของฉันด้วย divs ซึ่งเป็น div สำหรับแต่ละบท เป็นต้น ขณะนี้ไฟล์เป็นเพียงรายการแท็ก html (h1, p, h2 ฯลฯ) และฉันต้องการให้มันถูกต้องตามลำดับชั้นมากขึ้น

มีวิธีทำเช่นนั้นหรือไม่? ฉันพยายามใช้ตัวแบ่งหน้าและตัวเลือกอื่นที่คล้ายกัน แต่เพิ่มองค์ประกอบอื่นลงในรายการและไม่ล้อมเนื้อหาเฉพาะใน div ตามที่ฉันต้องการ โซลูชันจาวาสคริปต์ก็จะดีเช่นกัน

ขอขอบคุณสำหรับความช่วยเหลือใด ๆ ขอบคุณ! เนียร์


person nirshh5    schedule 01.03.2021    source แหล่งที่มา


คำตอบ (1)


คุณสามารถลองใช้วิธีนี้กับ Google Apps Script นี่ไม่ใช่วิธีแก้ปัญหาเดียวเท่านั้น นี่เป็นเพียงโค้ดง่ายๆ ที่คุณสามารถลองใช้ได้จากวิธีแก้ปัญหาที่เป็นไปได้มากมาย คุณสามารถปรับเปลี่ยนได้ตามต้องการ ขึ้นอยู่กับกรณีการใช้งานของคุณ

รหัส:

function myFunction() {
  var doc = DocumentApp.getActiveDocument();
  var content = doc.getBody();
  var numChildren = content.getNumChildren();
  var output = [];
  // Send email to this address
  var sendTo = "[email protected]"; 

  output.push("<html><body>");
  for(var i=0; i < numChildren; i++){
    var item = content.getChild(i).asText();
    var text = item.getText();

    // Assuming that a chapter always starts in bold headers, we start the div there
    if(item.isBold()) {
      // Add opening div tags on every start of header, see doc format below
      output.push('<div>');
      output.push('<h1>' + text + '</h1>');
    }
    // If not bold, then that element is assumed as the content of the chapter
    else if (text){
      output.push('<p>' + text + '</p>');
    }
  }
  output.push("</body></html>");

  // Get all indices where div is (except the first) and reverse
  var indexes = getAllIndexes(output, "<div>");
  // Per div found, insert closing div tag </div> before it
  indexes.forEach(function(index){
    output.splice(index, 0, "</div>");
  });

  // Join output array and treat it as html
  var html = Utilities.newBlob(output.join(""), 'text/html', 'doc_to_html.html');
  // Send to your email (modify email above if needed) the converted file with div tags
  MailApp.sendEmail(sendTo, "Attached html-converted document", "file is attached below", {name: 'doc-to-html', attachments: [html], noReply: true});
}

function getAllIndexes(arr, val) {
    var indexes = [], i = -1;
    while ((i = arr.indexOf(val, i+1)) != -1){
        indexes.push(i);
    }
    // Remove the first index (we don't need to add closing divs before the first div)
    indexes.shift();
    // Return reversed indices since we will add from the end since we are inserting closing div tags (</div>)
    // Inserting from the start will change the indices of those succeeding opening div tags (<div>) we need to close
    return indexes.reverse();
}

อีเมล:

เอาท์พุท1

ไฟล์แนบ HTML:

output2

บันทึก:

  • สันนิษฐานว่าในแต่ละบท มีส่วนหัวเดียวที่จุดเริ่มต้น (เราแทรก <div> ที่นี่) และย่อหน้าด้านล่าง แท็ก div ปิด </divs> จะถูกแทรกทุกแท็กก่อนพบแท็ก <div> ถัดไป
person NaziA    schedule 01.03.2021