function completeTable()

in excel-addin/src/taskpane/taskpane.js [370:457]


function completeTable() {
    var topic = try_fetch_topic();
    if (topic === null) {
        showToast("Please specify a topic (or click \"Generate a Random Topic\")")
        return;
    }
    showToast(`Completing a table for ${topic}...`, 4000);
    Excel.run(async function (context) {
        var sheet = context.workbook.worksheets.getActiveWorksheet();
        var range = context.workbook.getSelectedRange();
        range.load("values,rowIndex,columnIndex,rowCount,columnCount");
        await context.sync();

        if (range.rowCount < 2) {
            showToast("Please select a table area with at least 2 rows before clicking \"Complete Table\"");
            return;
        }

        const fields = range.values[0].filter(x => String(x).length);
        if (!fields.length) {
            showToast("Couldn't find header row - try clicking \"Suggest Header\" first.");
            return;
        }

        var completions = range.values.slice(1, range.rowCount);
        var partial = [];
        for(let i = 0; i < completions.length; i++) {
            var possible_partial = completions[i].filter(x => String(x).length);
            if (possible_partial.length < fields.length) {
                completions = completions.slice(0, i);
                partial = possible_partial;
                break;
            }
        }
        var side_info = try_fetch_side_info();
        var completions_obj = null;
        var temperature = 0.0;
        if (side_info === null) {
            completions_obj = await suggest_completions({topic: topic, fields: fields, completions: completions, partial: partial, temperature: temperature});
        }
        else {
            completions_obj = await suggest_completions({topic: topic, fields: fields, completions: completions, partial: partial, side_info: [side_info], temperature: temperature});
        }

        // set area as unbolded text
        range.numberFormat = "@";
        const unboldProps = {
            format: {
                font: {
                    bold: false
                }
            }
        };
        range.set(unboldProps);
        const boldProps = {
            format: {
                font: {
                    bold: true
                }
            }
        };
        sheet.getRangeByIndexes(range.rowIndex, range.columnIndex, 1, range.columnCount).set(boldProps);

        var max_tries = 3;
        for(let i = 0; i < max_tries; i++) {
            var pruned_completions = _maybe_prune_completions(completions_obj, fields.length, range.rowCount);
            var tmp_results = pruned_completions["result"].slice();
            tmp_results.unshift(fields);
            sheet.getRangeByIndexes(range.rowIndex, range.columnIndex, tmp_results.length, range.columnCount).values = tmp_results;
            await context.sync();
            if (pruned_completions['partial'].length && pruned_completions['partial'][0].startsWith('Please build a table')) {
                break;
            }
            if (tmp_results.length == range.rowCount) {
                break;
            }
            if (i < max_tries - 1) {
                showToast(`Completing a table for ${topic}...`);
                temperature += 0.7;
                completions_obj = await suggest_completions({topic: topic, fields: fields, completions: pruned_completions['result'], partial: pruned_completions['partial'], temperature: temperature});
            }
        }
        if (pruned_completions["result"].length + 1 != range.rowCount) {
            showToast("Could not complete table in one go; this usually means the selected area is too big. You can try clicking \"Complete Table\" again.")
        }
        return context.sync();
    }).catch(errorHandlerFunction);
}