Hướng dẫn convert text to html in google sheets - chuyển văn bản sang html trong google sheet

Tôi đã đi trước và cập nhật ý tưởng này để phản ánh tốt hơn cách chúng tôi làm điều này là năm 2021.

/**
 * @Author: Emma Sargent
 * Rich Text to HTML.
 * @param {Range} Google Sheets Range object
 * @returns {string} Text as HTML.
 * @customfunction
 */
function richTextToHtml(range) {
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }

Và bởi vì ai đó đã gửi email cho tôi và hỏi, đây là phiên bản có thể được chạy như một chức năng tùy chỉnh từ thanh công thức.

Bảng tính công thức: = RichTextToHTML ("yourSheetName! A1NotationRange")

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }

Bản demo TẠI ĐÂY: https://docs.google.com/spreadsheet/d/1x8i_lrxwouxwrkb2hzpztdiprs2nmxgtouwmnrkwjag/edit?usp=Shared


Tôi chưa xác minh điều này:
Read time: About 1 minute, or an average Caroline Konstnar song

Nói cách khác...

Nếu ô A1 trống, thì hãy trả lại "

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
1", nếu không, hãy trả lại nội dung của A1 được nối với một "

" được chuẩn bị và được bổ sung "

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
0".

  1. Sau đó, chỉ cần tự động điền cho nhiều ô như bạn cần.
  2. Đã trả lời ngày 24 tháng 5 năm 2017 lúc 19:56

Ngày 7 tháng 11 năm 2020 Đọc: Khoảng 1 phút, hoặc một bài hát Caroline Konstnar trung bình

  1. Google Sheets để chuyển đổi văn bản đơn giản thành mã HTML.
  2. Để sử dụng công cụ làm như sau.
  3. Cuộn và nhấp vào nút lớn màu đỏ: Get Text2HTML.
  4. Tạo một bản sao.
  5. Bây giờ bạn có một bản sao của riêng bạn. Đây là cách tôi sử dụng nó

Tôi chưa xác minh điều này:

Nói cách khác...main text with content such as this:

Nếu ô A1 trống, thì hãy trả lại "

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
1", nếu không, hãy trả lại nội dung của A1 được nối với một "

" được chuẩn bị và được bổ sung "

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
0".

Sau đó, chỉ cần tự động điền cho nhiều ô như bạn cần.

Đã trả lời ngày 24 tháng 5 năm 2017 lúc 19:56

Sau đó, chỉ cần tự động điền cho nhiều ô như bạn cần.

Đã trả lời ngày 24 tháng 5 năm 2017 lúc 19:56

Tôi đang sử dụng Google Sheets để giúp chuẩn bị các bài đăng cho các trang truyền thông xã hội khác nhau. Đối với một số trang web, tôi cần cắt và dán HTML vì các bài đăng của tôi chứa video YouTube và cách duy nhất để tránh sử dụng giao diện người dùng của trang web là đăng HTML trực tiếp.basic html) that references main text:

Paragraph 1


Paragraph 2


Paragraph 3

Vì vậy, trong trang của tôi, tôi có một văn bản chính cột với nội dung như thế này:

  • Đoạn 1
  • (dòng trống)

Đoạn văn bản 2

  • Đoạn 3
  • Nếu có một dòng trống, hãy chuyển đổi nó thành thẻ
    function richTextToHtml(rangeStr) {
      let [sheetName, rangeName] = rangeStr.split("!");
      sheetName = sheetName.replaceAll("'",'');
      const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
      const runs = range.getRichTextValue().getRuns();
      const formattedRuns = runs.map((run) => {
        const attr = {
          style: '',
        };
        const text = run.getText();
        const link = run.getLinkUrl();
        let parentTag = 'span';
        if (link) {
          parentTag = 'a';
          attr.href = link;
        }
    
        const style = run.getTextStyle();
        const styles = {
          'font-family': `'${style.getFontFamily()}'`,
          'font-size': `${style.getFontSize()}px`,
          color: style.getForegroundColor(),
        };
        attr.style = Object.entries(styles)
          .map(([key, val]) => `${key}: ${val}`)
          .join('; ');
    
        let tags = [];
        if (style.isBold()) {
          tags.push('b');
        }
        if (style.isItalic()) {
          tags.push('i');
        }
        if (style.isUnderline()) {
          tags.push('u');
        }
        if (style.isStrikethrough()) {
          tags.push('strike');
        }
        const headTags = tags.length ? `<${tags.join('><')}>` : '';
        const closeTags = tags.length ? `` : '';
        const attrStr = Object.entries(attr)
          .map(([key, val]) => `${key}="${val}"`)
          .join(' ');
    
        const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
        const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
    '); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
    1.

Tôi đã đọc qua danh sách các chức năng của Google Sheets và tôi có một số sự quen thuộc cơ bản với Trình chỉnh sửa tập lệnh, nhưng tôi không biết nên thực hiện hướng nào để thực hiện việc này.

Khi được hỏi ngày 24 tháng 5 năm 2017 lúc 18:45May 24, 2017 at 18:45

Đó không phải là một công thức khá, nhưng tôi đã làm việc này (thay thế

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
2 bằng ô có văn bản nguồn để thực hiện các sửa đổi trên):

=SUBSTITUTE(CONCATENATE("

", SUBSTITUTE(SUBSTITUTE(A1, " ", "
"), " ", "

"), "

"), "


", "
")

Hướng dẫn convert text to html in google sheets - chuyển văn bản sang html trong google sheet

Lưu ý rằng dòng phá vỡ trong công thức là quan trọng. Điều này cũng giả định rằng dòng đầu tiên của ô không phải là một dòng trống.

Đã trả lời ngày 24 tháng 5 năm 2017 lúc 20:16May 24, 2017 at 20:16

Hướng dẫn convert text to html in google sheets - chuyển văn bản sang html trong google sheet

Đây là một giải pháp sẽ hoạt động nếu tất cả các văn bản nằm trong một ô duy nhất và không yêu cầu phá vỡ dòng vui nhộn trong công thức.

=concatenate("

",substitute(A1,CHAR(10)&CHAR(10),concatenate("

",CHAR(10),"
",CHAR(10),"

")),"

")

Hướng dẫn convert text to html in google sheets - chuyển văn bản sang html trong google sheet

Tuy nhiên, điều này giả định rằng các đoạn văn được phân tách bằng một ký tự nguồn cấp dữ liệu thông thường (

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
3), mà bạn thường có thể nhận được trong Google Sheets bằng cách sử dụng CTRL+ENTER.

Đã trả lời ngày 24 tháng 5 năm 2017 lúc 20:28May 24, 2017 at 20:28

alealeale

51.4K40 Huy hiệu vàng158 Huy hiệu bạc301 Huy hiệu đồng40 gold badges158 silver badges301 bronze badges

Tôi chưa xác minh điều này:

=IF(ISBLANK(A1),"
",CONCATENATE("

",A1,"

"))

Nói cách khác...

Nếu ô A1 trống, thì hãy trả lại "

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
1", nếu không, hãy trả lại nội dung của A1 được nối với một "

" được chuẩn bị và được bổ sung "

function richTextToHtml(rangeStr) {
  let [sheetName, rangeName] = rangeStr.split("!");
  sheetName = sheetName.replaceAll("'",'');
  const range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(rangeName);
  const runs = range.getRichTextValue().getRuns();
  const formattedRuns = runs.map((run) => {
    const attr = {
      style: '',
    };
    const text = run.getText();
    const link = run.getLinkUrl();
    let parentTag = 'span';
    if (link) {
      parentTag = 'a';
      attr.href = link;
    }

    const style = run.getTextStyle();
    const styles = {
      'font-family': `'${style.getFontFamily()}'`,
      'font-size': `${style.getFontSize()}px`,
      color: style.getForegroundColor(),
    };
    attr.style = Object.entries(styles)
      .map(([key, val]) => `${key}: ${val}`)
      .join('; ');

    let tags = [];
    if (style.isBold()) {
      tags.push('b');
    }
    if (style.isItalic()) {
      tags.push('i');
    }
    if (style.isUnderline()) {
      tags.push('u');
    }
    if (style.isStrikethrough()) {
      tags.push('strike');
    }
    const headTags = tags.length ? `<${tags.join('><')}>` : '';
    const closeTags = tags.length ? `` : '';
    const attrStr = Object.entries(attr)
      .map(([key, val]) => `${key}="${val}"`)
      .join(' ');

    const mainTag = `<${parentTag} ${attrStr}>${headTags}${text}${closeTags}`;
    const lineBreakFormattedStr = mainTag.replace(/[\r\n]/g, '
'); return lineBreakFormattedStr; }); return formattedRuns.join(''); }
0".

Hướng dẫn convert text to html in google sheets - chuyển văn bản sang html trong google sheet

Sau đó, chỉ cần tự động điền cho nhiều ô như bạn cần.

Đã trả lời ngày 24 tháng 5 năm 2017 lúc 19:56May 24, 2017 at 19:56

alealeale

51.4K40 Huy hiệu vàng158 Huy hiệu bạc301 Huy hiệu đồng40 gold badges158 silver badges301 bronze badges

3