从电子表格调用时出错,从菜单中正常工作

时间:2012-05-27 17:52:01

标签: google-apps-script google-sheets custom-function

所以我在Google Apps Scripts首次发布时做了一些工作,但最近我没有和他们合作过。今天早上,我决定开发一个新脚本。一切看起来都很棒,我喜欢这些新功能。然而,之前有效的一件非常简单的事情现在已被打破(或者我只是愚蠢)。

当我从菜单调用特定功能时,它可以正常工作。但是,当我直接从电子表格中调用它时,我得到:

您无权调用inputBox(第17行)

奇怪的是,我甚至没有调用inputBox。有一组事件可以调用输入框,但只有在没有设置特定变量(全局)的情况下才会调用它,在这种情况下它被设置。

我想也许我只是有一个错误。所以,我查看了我的旧应用程序脚本(一次肯定有效)。我现在遇到了同样的问题。所以,无论改变什么,它都会破坏旧脚本。为了完整性,我现在包括我的脚本代码。我试图直接调用的函数是Pipeline和Activity:

var webhookurl = "";

var jsondata = [];

function __urlCheck_(str) {
    var v = new RegExp(); 
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(str)) {
        return false;
    }
    return true;
}

function __getWebHook_(){
    var newwebhookurl = UserProperties.getProperty('webhookurl_HCR');
    if((newwebhookurl == null || newwebhookurl.length==0) && (webhookurl.length==0 || webhookurl==null)){
        var newwebhookurl = Browser.inputBox("Paste your web hook url here");
        var newwebhookurl = newwebhookurl.replace(/^\s*/, '').replace(/\s*$/, '');
        if('cancel' == newwebhookurl.toLowerCase()){
            Browser.msgBox('Before this connection will work, you will have to enter a webhook url.');
        }else if(newwebhookurl.length==0 || newwebhookurl == null || __urlCheck_(newwebhookurl) == false){
            Browser.msgBox('Sorry, you must specify a webhook url.');
            __getWebHook_();
        }else{
            UserProperties.setProperty('webhookurl_HCR',newwebhookurl);
            webhookurl = newwebhookurl;
        }
    }else if(webhookurl.length==0 || webhookurl==null){
        webhookurl = newwebhookurl;
    }
}


function __ClearWebHook_(){
    UserProperties.setProperty('webhookurl_HCR','');
    webhookurl = '';
    __getWebHook_();
}


function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [{name: "Pipeline", functionName: "__PipelineMenu__"}, 
                     {name: "Activity", functionName: "__ActivityMenu__"},
                     {name: "Reset WebHook", functionName: "__ClearWebHook_"}
                     ];
  ss.addMenu("Heap CRM Reports", menuEntries);

}


function onInstall(){
    onOpen();
}

function __PipelineMenu__(){
    var mydoc = SpreadsheetApp.getActiveSpreadsheet();
    var myapp = UiApp.createApplication().setTitle('Pipeline');
    var mygrid = myapp.createGrid(5, 2);
    mygrid.setWidget(0, 0, myapp.createLabel('Category:'));
    mygrid.setWidget(0, 1, myapp.createTextBox().setName('category'));
    mygrid.setWidget(1, 0, myapp.createLabel('Label:'));
    mygrid.setWidget(1, 1, myapp.createTextBox().setName('label'));
    mygrid.setWidget(2, 0, myapp.createLabel('User:'));
    mygrid.setWidget(2, 1, myapp.createTextBox().setName('user'));
    mygrid.setWidget(3, 0, myapp.createLabel('Type:'));
    var lbt = myapp.createListBox(false).setName('type');
    lbt.setVisibleItemCount(1);
    lbt.addItem('Lead');
    lbt.addItem('Opportunity');
    lbt.addItem('Customer');
    mygrid.setWidget(3, 1, lbt);
    mygrid.setWidget(4, 0, myapp.createLabel('Kind:'));
    var lbk = myapp.createListBox(false).setName('kind');
    lbk.setVisibleItemCount(1);
    lbk.addItem('Expected Value');
    lbk.addItem('Value');
    lbk.addItem('Count');
    mygrid.setWidget(4, 1, lbk);
    var panel = myapp.createVerticalPanel();
    panel.add(mygrid);
    var button = myapp.createButton('Submit');
    var handler = myapp.createServerClickHandler('__processPipeline');
    handler.addCallbackElement(mygrid);
    button.addClickHandler(handler);
    panel.add(button);
    myapp.add(panel);
    mydoc.show(myapp);
}


function __ActivityMenu__(){
    var mydoc = SpreadsheetApp.getActiveSpreadsheet();
    var myapp = UiApp.createApplication().setTitle('Activity');
    var mygrid = myapp.createGrid(7, 2);
    mygrid.setWidget(0, 0, myapp.createLabel('Category:'));
    mygrid.setWidget(0, 1, myapp.createTextBox().setName('category'));
    mygrid.setWidget(1, 0, myapp.createLabel('Label:'));
    mygrid.setWidget(1, 1, myapp.createTextBox().setName('label'));
    mygrid.setWidget(2, 0, myapp.createLabel('User:'));
    mygrid.setWidget(2, 1, myapp.createTextBox().setName('user'));
    mygrid.setWidget(3, 0, myapp.createLabel('Date:'));
    mygrid.setWidget(3, 1, myapp.createDateBox().setId('date'));
    mygrid.setWidget(4, 0, myapp.createLabel('Range:'));
    var lb = myapp.createListBox(false).setName('range');
    lb.setVisibleItemCount(1);
    lb.addItem('Week');
    lb.addItem('Month');
    lb.addItem('Year');
    mygrid.setWidget(4, 1, lb);
    mygrid.setWidget(5, 0, myapp.createLabel('Type:'));
    var lbt = myapp.createListBox(false).setName('type');
    lbt.setVisibleItemCount(1);
    lbt.addItem('Lead');
    lbt.addItem('Opportunity');
    lbt.addItem('Customer');
    mygrid.setWidget(5, 1, lbt);
    mygrid.setWidget(6, 0, myapp.createLabel('Kind:'));
    var lbk = myapp.createListBox(false).setName('kind');
    lbk.setVisibleItemCount(1);
    lbk.addItem('Expected Value');
    lbk.addItem('Value');
    lbk.addItem('Count');
    mygrid.setWidget(6, 1, lbk);
    var panel = myapp.createVerticalPanel();
    panel.add(mygrid);
    var button = myapp.createButton('Submit');
    var handler = myapp.createServerClickHandler('__processActivity');
    handler.addCallbackElement(mygrid);
    button.addClickHandler(handler);
    panel.add(button);
    myapp.add(panel);
    mydoc.show(myapp);
}


function __processActivity(inputvalues){
    var category = inputvalues.parameter.category;
    var label = inputvalues.parameter.label;
    var user = inputvalues.parameter.user;
    var date = inputvalues.parameter.date;
    var range = inputvalues.parameter.range;
    var type = inputvalues.parameter.type;
    var kind = inputvalues.parameter.kind;
    var answer = Activity(kind,type,date,range,category,user,'',label);
    var mysheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    mysheet.getActiveCell().setValue(answer);
    var app = UiApp.getActiveApplication();
    app.close();
    return app;
}

function Activity(kind,type,date,range,category,user,mylabel,label){
    var kind = kind || '';
    var type = type || '';
    var category = category || '';
    var user = user || '';
    var mylabel = mylabel || '';
    var label = label || '';
    var range = range || '';
    var date = date || '';
    var data = __getData_('activity ' + kind,category,user,label,mylabel,date,range);
    var type = type.toLowerCase();
    if(type=='lead' && data.hasOwnProperty('lead')==true){
        return data['lead'];
    }else if(type=='customer' && data.hasOwnProperty('customer')==true){
        return data['customer'];
    }else if(type=='opportunity' && data.hasOwnProperty('opportunity')==true){
        return data['opportunity'];
    }else{
        return 0;
    }
}

function Pipeline(kind,type,category,user,mylabel,label){
    var kind = kind || '';
    var type = type || '';
    var category = category || '';
    var user = user || '';
    var mylabel = mylabel || '';
    var label = label || '';
    var data = __getData_('pipeline ' + kind,category,user,label,mylabel);
    var type = type.toLowerCase();
    if(type=='lead' && data.hasOwnProperty('lead')==true){
        return data['lead'];
    }else if(type=='customer' && data.hasOwnProperty('customer')==true){
        return data['customer'];
    }else if(type=='opportunity' && data.hasOwnProperty('opportunity')==true){
        return data['opportunity'];
    }else{
        return 0;
    }
}

function __processPipeline(inputvalues){
    var category = inputvalues.parameter.category;
    var label = inputvalues.parameter.label;
    var user = inputvalues.parameter.user;
    var type = inputvalues.parameter.type;
    var kind = inputvalues.parameter.kind;
    var answer = Pipeline(kind,type,category,user,'',label);
    var mysheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    mysheet.getActiveCell().setValue(answer);
    var app = UiApp.getActiveApplication();
    app.close();
    return app;
} 

function __getData_(mlist,mcategory,muser,label,mlabel,mdate,mrange) {
    var addon = '';
    var mlist = mlist || '';
    var mcategory = mcategory || '';
    var muser = muser || '';
    var label = label || '';
    var mlabel = mlabel || '';
    var mdate = mdate || '';
    var mrange = mrange || '';
    if(mcategory.length>0){
        addon = addon + "&category=" + encodeURIComponent(mcategory);
    }
    if(muser.length>0){
         addon = addon + "&user=" + encodeURIComponent(muser);
    }
    if(mlabel.length>0){
         addon = addon + "&mylabel=" + encodeURIComponent(mlabel);
    }
    if(label.length>0){
        addon = addon + "&label=" + encodeURIComponent(label);
    }
    if(mdate.length>0){
         addon = addon + "&date=" + encodeURIComponent(mdate);
    }
    if(mrange.length>0){
        addon = addon + "&range=" + encodeURIComponent(mrange);
    }
    __getWebHook_();

    if(webhookurl.length==0 || webhookurl == null){
        return [];
    }
    addon = "&list=" + encodeURIComponent(mlist) + addon;
    for(i=0;i<jsondata.length;i++){
        if(jsondata[i]['addon'].toLowerCase() == addon.toLowerCase()){
            return jsondata[i]['json'];
        }
    }

    var url = webhookurl + addon;
    var response = UrlFetchApp.fetch(url);
    if (response.getResponseCode() == 200) {
        var newid = jsondata.length;
        jsondata[newid] = {};
        jsondata[newid].json = Utilities.jsonParse(response.getContentText());
        jsondata[newid].addon = addon.toLowerCase();
        return jsondata[newid].json;
    }
}

1 个答案:

答案 0 :(得分:2)

这与:

相同

No permission to call msgBox in Google Apps Scripting

自定义函数除了返回可能影响连续单元格的数组外,只会将值返回给它们所在的单元格。

自定义函数无法启动输入框(Browser.inputBox())

自定义函数是在电子表格单元格中作为全部或部分公式输入的函数,该函数从脚本编辑器调用函数。

如你所说:'过去曾经工作'但已被禁用