Help us understand the problem. What is going on with this article?

Google Spreadsheet の内容を Markdown Table に変換

More than 1 year has passed since last update.

動作イメージ

Screen Shot 2016-12-28 at 17.36.03.png

Screen Shot 2016-12-28 at 17.36.19.png

| ID | 名前 | 個数 |
|:---:|:---:|:---:|
| 1 | りんご | 8 |
| 2 | バナナ | 274 |
| 3 | 全ての答え | 42 |
ID 名前 個数
1 りんご 8
2 バナナ 274
3 全ての答え 42

コード

少し冗長な書き方をしていますが、メインは sheetToMarkdownTable() のところです。
手抜きなので常に 1 行目をヘッダとして扱い、文字列は中央寄せ固定にしています。

function onOpen() {
    SpreadsheetApp.getUi().createMenu('変換')
        .addItem('シート -> Markdown Table', 'sheetToMarkdownTable')
        .addToUi();
}

var ns = {};
(function() {
    ns.sheets = {};
    ns.sheets.data = {
        instance: SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data"),
        ranges: {},
        fn: {}
    };

    ns.app = {};
    ns.app.fn = {};
})();

(function() {
    ns.app.fn.sheetToMarkdownTable = function() {
        var sheet = ns.sheets.data.instance;
        var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());

        var values = range.getValues();

        var outputs = [];
        values.forEach(function(cols, index) {
            if (index === 0) {
                var header = '| ' + cols.join(' | ') + ' |';
                outputs.push(header);
                outputs.push(Array(cols.length + 1).join('|:---:') + '|');
                return;
            }

            outputs.push('| ' + cols.join(' | ') + ' |');
        });

        Browser.msgBox(outputs.join('\\n'));
    };
})();

function sheetToMarkdownTable() {
    return ns.app.fn.sheetToMarkdownTable();
};

GAS 側でシート名を data と固定にしている都合上、シートの名前をシート1から data に変更する必要があります。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away