Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceaksan/4ff3b4a170514edc28f189c38e99bee6 to your computer and use it in GitHub Desktop.
Save ceaksan/4ff3b4a170514edc28f189c38e99bee6 to your computer and use it in GitHub Desktop.
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('View more cell actions')
.addItem('Convert table to Markdown', 'convertTableToMarkdown')
.addToUi();
}
function convertTableToMarkdown() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const range = sheet.getDataRange();
const values = range.getValues();
const numRows = range.getNumRows();
const numCols = range.getNumColumns();
const columnNames = sheet.getRange(1, 1, 1, numCols).getValues()[0].map(String);
const piiColumns = ['Email_pii', 'Address_pii']; // Replace with your desired column names for array matching
let markdownTable = '';
for (let i = 0; i < numRows; i++) {
for (let j = 0; j < numCols; j++) {
const columnName = columnNames[j];
const isPiiColumn = piiColumns.includes(columnName);
if (isPiiColumn) {
values[i][j] = '***';
}
markdownTable += `|${values[i][j]}`;
}
markdownTable += '|\n';
if (i === 0) {
const horizontalLine = Array.from({ length: numCols }).fill('---').join('|');
markdownTable += `${horizontalLine}|\n`;
}
}
Logger.log(markdownTable);
// Show the Markdown table in a dialog box
const htmlOutput = HtmlService.createHtmlOutput(`<pre>${markdownTable}</pre>`);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Markdown Table');
}
@ceaksan
Copy link
Author

ceaksan commented Jul 16, 2023

function convertTableToMarkdown() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const range = sheet.getDataRange();
  const values = range.getValues();
  const numRows = range.getNumRows();
  const numCols = range.getNumColumns();
  const columnNames = sheet.getRange(1, 1, 1, numCols).getValues()[0].map(String);
  const piiColumns = ['Email_pii', 'Adress_pii']; // Replace with your desired column names for array matching

  let markdownTable = '';

  for (let i = 0; i < numRows; i++) {
    for (let j = 0; j < numCols; j++) {
      const columnName = columnNames[j];
      const isPiiColumn = piiColumns.includes(columnName);

      if (isPiiColumn) {
        values[i][j] = '***';
      }

      markdownTable += `|${values[i][j]}`;
    }

    markdownTable += '|\n';

    if (i === 0) {
      const horizontalLine = Array.from({ length: numCols }).fill('---').join('|');
      markdownTable += `${horizontalLine}|\n`;
    }
  }

  Logger.log(markdownTable);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment