1

I'm trying to understand how to wrap text in SheetJS to be able to have long texts in a cell split in multiline.

So far whatever I tried didn't work

const generateMasterReport = data => {
  // Workbook
  const wb = XLSX.utils.book_new();

  // Worksheet
  const formattedData = data.map(rawToHeaders);
  const ws = XLSX.utils.json_to_sheet(formattedData, {
    origin: 'A2',
    cellDates: true,
    dateNF: DATE_FORMAT,
  });

  // Header groups
  ws['!merges'] = headerGroups.map(({ start, end }) =>
    XLSX.utils.decode_range(`${start}:${end}`),
  );
  headerGroups.forEach(({ name, start }) => {
    XLSX.utils.sheet_add_aoa(ws, [[name]], { origin: start });
  });

  // Column widths
  ws['!cols'] = Object.keys(formattedData[0] ?? {}).map(header => {
    const headerLength = header.length;
    if (headerLength < MIN_WIDTH) {
      return { wch: MIN_WIDTH };
    }
    if (headerLength > MAX_WIDTH) {
      return { wch: MAX_WIDTH };
    }
    return { wch: headerLength + WIDTH_MARGIN };
  });

  ws['!cols'] = { alignment: { wrapText: '1' } };

  XLSX.utils.book_append_sheet(wb, ws);

  return XLSX.write(wb, {
    type: 'buffer',
    bookType: 'xlsx',
  });
};

I have no idea where and how should I do this style change to wrap the text in the above code

I want to avoid the following issue and make the text to be multiline

enter image description here

Jakub
  • 2,367
  • 6
  • 31
  • 82

0 Answers0