Range To Markdown Table

rangeToMarkdownTable(range) turns a sheet range into a Markdown table, using display values so dates and numbers look the way they do in the grid. The first row is the header. Pipe characters inside cells are escaped, and line breaks become spaces, so the table pastes cleanly into Slack or GitHub.

When to use

  • Pasting a small summary into Slack
  • Dropping a range into a GitHub issue or pull request
  • Any hand-off that wants a table, not a screenshot

How to use this snippet

/** * Build a Markdown table from a range's display values. * The first row is the header. Pipes are escaped; newlines become spaces. * * @param {GoogleAppsScript.Spreadsheet.Range} range * @return {string} */ function rangeToMarkdownTable(range) { var values = range.getDisplayValues(); if (!values.length || !values[0].length) return ''; function cell(v) { return String(v == null ? '' : v).replace(/\|/g, '\\|').replace(/\r?\n/g, ' '); } var cols = values[0].length; var header = []; for (var c = 0; c < cols; c++) header.push(cell(values[0][c])); var lines = ['| ' + header.join(' | ') + ' |']; var rule = []; for (var r = 0; r < cols; r++) rule.push('---'); lines.push('| ' + rule.join(' | ') + ' |'); for (var i = 1; i < values.length; i++) { var row = []; for (var j = 0; j < cols; j++) row.push(cell(values[i][j])); lines.push('| ' + row.join(' | ') + ' |'); } return lines.join('\n'); }

Example

function copySummaryMarkdown() { var range = SpreadsheetApp.getActive().getSheetByName('Summary').getDataRange(); var md = rangeToMarkdownTable(range); Logger.log(md); // paste the log into Slack or GitHub }

Notes

  • Empty ranges return ''.
  • This is text, not a live link back to the sheet. Numbers will not change when the sheet does.
  • Wide ranges make clumsy Slack messages. Copy a tight summary range, not the whole tab.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free snippets — plus optional Co-Pilot when you want a boost (Day Pass — $3, or 1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension