忍者ブログ

ひつ(じのひよこが)プログラミングします。
お仕事や趣味で困ったこととか、何度も「あれ?どうだったかしら」と調べたりしたこととか、作ったものとか、こどものこととかを書きます
★前は週末定期更新でしたが今は不定期更新です

2024/05    04« 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  »06

Google Spreadsheet の装飾を HTML 化する

GAS で書いた。

function myFunction() {
  const styles = [
    {
      name: 'getFontSize',
      func: (val)=>{if(val) {return `font-size:${val}px;`}}
    }, {
      name: 'getForegroundColor',
      func: (val)=>{if(val) {return `color:${val};`}}
    }, {
      name: 'isBold',
      func: (val)=>{if(val) {return `font-weight:bold;`}}
    }, {
      name: 'isItalic',
      func: (val)=>{if(val) {return 'font-style: italic;';}}
    }, {
      name: 'isStrikethrough',
      func: (val)=>{if(val) {return 'text-decoration: line-through ';} else {return 'text-decoration: ';}}
    }, {
      name: 'isUnderline',
      func: (val)=>{if(val) {return 'underline;';} else {return ';';}}
    }
  ];

  const sheet = SpreadsheetApp.getActiveSheet();
  const cell = sheet.getRange(1, 1);
  const elements = cell.getRichTextValue().getRuns();
  const html = elements.map((element)=>{
    const style = styles.map((style)=>{
      return style.func(element.getTextStyle()[style.name]()) || '';
    }).join('');
    return `<span style="${style}">${element.getText().replaceAll('\n', '<br/>')}</span>`;
  }).join('');
  console.log(html);
}
PR

コメント

ただいまコメントを受けつけておりません。

ブログ内検索

P R