My google script which based on Google Sheet and Form

My google script which based on Google Sheet and Form


// get sheet data
function getSpreadsheetData(sheetId) {
    // This function gives you an array of objects modeling a worksheet's tabular data, where the first items — column headers — become the property names.
    var arrayOfArrays = SpreadsheetApp.openById(sheetId).getDataRange().getValues();
    var headers = arrayOfArrays.shift();
    return arrayOfArrays.map(function(row) {
        return row.reduce(function(memo, value, index) {
            if (value) {
                memo[headers[index]] = value;
            }
            return memo;
        }, {});
    });
}

// class definition
function instance(appName, url, oldLineNum, newLineNum, importedLibrary, author) {
    var o = new Object();
    o.appName = appName;
    o.url = url;
    o.oldLineNum = oldLineNum;
    o.newLineNum = newLineNum;
    o.importedLibrary = importedLibrary;
    o.author = author;
    return o;
}

function getInstFromRow(row) {
    return new instance(row['appName'], row['url'], row['oldLineNum'], row['newLineNum'], row['importedLibrary'], row['author']);
}

function main() {

    var sheetId = '1ixlsO8claL7KMouUe8S8GKhn_yZI6yVh-A8jvbzf71A'

    var instances = getSpreadsheetData(sheetId);
    while (instances.length != 0) {
        var curInstanceList = [];
        // get head
        var curInst = getInstFromRow(instances[0]);
        curInstanceList.push(curInst);
        var tmpInstances = [];
        for (var index = 1; index < instances.length; index++) {
            var tmpInst = getInstFromRow(instances[index]);
            // if belongs to same author
            if (tmpInst.author == curInst.author) {
                curInstanceList.push(tmpInst);
            } else {
                tmpInstances.push(tmpInst);
            }
        }
        instances = tmpInstances
        // get all instances with same author
        var formName = 'Survey on redundant implementations between Android apps and APIs';
        var form = FormApp.create(curInst.author + ' - ' + formName);

        form.setTitle(formName);
        form.setDescription('We are going to show the customized diff patch here ... FromFoutse(you need to have a section describing your study!)  and since you found code snippets from their code you may want to show it to them and formulate specific questions to understand their motivations for refactoring,...etc)');

        // sect1
        var sect1 = form.addSectionHeaderItem();
        sect1.setTitle('Example(s):');
        var str = ''

        while (curInstanceList.length != 0) {
            // get head
            var instTmpHead = curInstanceList[0];
            str += ('Patch Url : \n' + instTmpHead.url + '\nOld Line Num :\n' + instTmpHead.oldLineNum + '\nNew Line Num :\n' + instTmpHead.newLineNum + '\nImported Library :\n' + instTmpHead.importedLibrary + '\n');
            var tmpCurInstanceList = []
            for (var i = 1; i < curInstanceList.length; i++) {
                var instTmp = curInstanceList[i];
                // if belongs to same url
                if (instTmpHead.url == instTmp.url) {
                    str += ('Old Line Num : \n' + instTmp.oldLineNum + '\nNew Line Num :\n' + instTmp.newLineNum + '\nImported Library :\n' + instTmp.importedLibrary + '\n');
                } else {
                    tmpCurInstanceList.push(instTmp);
                }
            }
            curInstanceList = tmpCurInstanceList;
        }
        //var item11 = form.addTextItem();
        //item11.setTitle(str);
        sect1.setHelpText(str)

        // sect2
        form.addPageBreakItem().setTitle('Section 1 of 3');
        var sect2 = form.addSectionHeaderItem();
        var item21 = form.addMultipleChoiceItem().setTitle('Do you use the API call to refactor your original implementation?')
        item21.setChoices([item21.createChoice('Yes'), item21.createChoice('No'), ]).showOtherOption(true);
        var item22 = form.addMultipleChoiceItem().setTitle('What is the reason why you did not use the API method in the first place?');
        item22.setChoices([item22.createChoice('I did not know about this API method when I was implementing the code.'), item22.createChoice('The required API method had not been introduced at the moment of my implementation.'), ]).showOtherOption(true);
        var item23 = form.addMultipleChoiceItem().setTitle('Why did you replace your code with an API call?');
        item23.setChoices([item23.createChoice('Because I want to more easily maintain my code.'), item23.createChoice('Because I want to have a higher performance.'), ]).showOtherOption(true);
        var item24 = form.addMultipleChoiceItem().setTitle('Do you actively search for API reuse opportunities (i.e., code that can be replaced by API calls)?');

        // sect3
        var page3 = form.addPageBreakItem().setTitle('Section 2 of 3');
        var sect3 = form.addSectionHeaderItem().setTitle('In case you actively search for API reuse opportunities');
        var item31 = form.addTextItem();
        item31.setTitle('How do you perform such search?');

        // sect4
        var page4 = form.addPageBreakItem().setTitle('Section 2 of 3');;
        var sect4 = form.addSectionHeaderItem().setTitle('In case you do not actively search for API reuse opportunities');
        var item41 = form.addTextItem();
        item41.setTitle('Why don\'t you search such opportunities?');

        var choice1 = item24.createChoice('Yes', page3);
        var choice2 = item24.createChoice('No', page4);
        item24.setChoices([choice1, choice2]);

        // sect5
        var page5 = form.addPageBreakItem().setTitle('Section 3 of 3');;
        var sect5 = form.addSectionHeaderItem();
        sect5.setTitle('How difficult to find an appropriate API to replace my code?')
        var item51 = form.addMultipleChoiceItem();
        item51.setTitle('Do you find it challenging to find API reuse opportunities?')
            .setChoices([item51.createChoice('Yes'), item51.createChoice('No'), ]);
        var item52 = form.addTextItem()
        item52.setTitle('Which criteria do you consider when replacing a piece of your own code with a corresponding API implementation?')

        page4.setGoToPage(page5);
    }
}

处理sheet的数据时,里面还有一些自己的逻辑。懒得改了,大家挑自己有用的用吧。

Sheet失效了请联系我。

https://docs.google.com/spreadsheets/d/1ixlsO8claL7KMouUe8S8GKhn_yZI6yVh-A8jvbzf71A/edit#gid=0

其中,1ixlsO8claL7KMouUe8S8GKhn_yZI6yVh-A8jvbzf71A就是sheetId。

posted @ 2017-08-16 15:18  max_xbw  阅读(438)  评论(0编辑  收藏  举报