【问题标题】:Error when submitting form to spread sheet using app script使用应用脚本将表单提交到电子表格时出错
【发布时间】:2013-08-29 18:11:33
【问题描述】:

我已经创建了这个脚本,它制作了一个表单来发布一个简介,我希望它被提交到一个电子表格,但是我一直收到这个错误Exception: incorrect range width, was 3 but should be 5不管我改变了 getRange 中的行数这个数字错误总是一样的。有什么方法可以更新我不知道的代码吗?我每次更改代码时都会部署代码。

  function doGet() {
  var app = UiApp.createApplication().setTitle('Form for news update');

  //panel for form
  var panel = app.createVerticalPanel().setId('panel');

  //elements for the form
  var postTitle = app.createLabel('Title');
  var title = app.createTextBox().setId('title');
  var postLabel = app.createLabel('new post:');
  var post = app.createTextArea().setId('post');
  var btn = app.createButton('Submit');

  //handler to execute posting by click the button

  var handler = app.createServerClickHandler('Submit');
  handler.addCallbackElement(panel);
  //add this handler to the button
  btn.addClickHandler(handler);

  //add the elements to the panel
  panel.add(postTitle)
  .add(title)
  .add(postLabel)
  .add(post)
  .add(btn);

  //add the panel to the app
  app.add(panel);

  return app;
}
function Submit(e){
//get the app and send it to the spreadsheet
var app = UiApp.getActiveApplication();

try{
   //get the post
  var postTitle = e.parameter.postTitle;
  var title = e.parameter.title;
  var post = e.parameter.post;

  //put the info into a spreadsheet
  var ss = SpreadsheetApp.openById('KEY IN HERE REMOVED FOR PRIVACY');
  var sheet = ss.getSheets()[0];
  sheet.getRange(sheet.getLastRow()+1, 1).setValues([[ title, post]]);
}catch(e){
  app.add(app.createLabel('Error occured:'+e));
 return app;
}
}

【问题讨论】:

    标签: google-apps-script google-sheets google-forms


    【解决方案1】:

    如果您的二维数组未设置为 100% 四边形且与所选范围未 100% 匹配,也会发生此错误。

    例如,如果你有一个数组:

    [
    [a,b,c,d,e,f,g],
    [a,b,c,d,e,f,g],
    [a,b,c,d,e,f]
    ]
    

    它会给你一个错误说Exception: incorrect range width, was 7 but should be 6

    解决方案当然是用空值填充多余的单元格:

    [
    [a,b,c,d,e,f,g],
    [a,b,c,d,e,f,g],
    [a,b,c,d,e,f,''],
    ]
    

    基本上,确保它们都不比任何其他数组长。

    【讨论】:

      【解决方案2】:

      您选择了一个具有起始位置(行、列)但没有指定高度(行)或宽度(列)的范围。它将默认为 1 x 1。然后 setValues 方法尝试应用一个二维数组,该数组是一组不同的维度,在此示例中为 1 行 x 2 列:

      sheet.getRange(sheet.getLastRow()+1, 1).setValues([[ title, post]]);
                     --------------------  -            ----------------
                       |                   +- column         |
                       +- row                                +- 1 row, 2 columns
      

      当异常报告width 时,将其等同于列,而不是行。

      您应该使用.getRange(row, column, numRows, numColumns),如:

      sheet.getRange(sheet.getLastRow()+1, 1, 1, 2).setValues([[ title, post]]);
      

      为了提高维护,尽量避免使用幻数。

      var outData = [[ postTitle, title, post ]];
      sheet.getRange(sheet.getLastRow()+1, 1, outData.length, outData[0].length)
           .setValues(outData);
      

      这样,如果您更改要存储到工作表的数据,则无需维护将其写出的行。相反,它将根据数据维度计算正确的维度。

      最后一句话:去掉那个 try..catch 块,你不需要它,它会导致错误,比如不小心将你的 return app 包含在 catch 块中,可能会导致你的应用程序静默失败。

      【讨论】:

      • 非常感谢您的反馈,信息量超级大!有什么好的读物可以推荐吗?我应该用什么代替try..catch?抱歉,我昨天才开始使用应用脚本
      • 它有助于首先学习 Javascript,而不需要像 jQuery 这样的网络技术。例如,来自 Codeacademy 的在线学习。各种教程很有帮助。通过在这里回答问题,我学到的知识比我自己使用 GAS 学到的更多,所以我建议您尝试一下,即使只是作为练习。 WRT try..catch,请参阅我在 this previous answer 中的笔记。仅将其用于您无法预期或以其他方式处理的错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 2023-02-01
      • 2021-11-11
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多