google-apps-script - 使用应用程序脚本将表单提交到电子表格时出错

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

我已经创建了这个脚本来制作一个表单来发布一个简介,我希望它被提交到一个电子表格,但我一直收到这个错误异常:不正确的范围宽度,是 3 但应该是 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;
}
}

最佳答案

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

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

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

它会给你一个错误提示 Exception: wrong 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,''],
]

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

关于google-apps-script - 使用应用程序脚本将表单提交到电子表格时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18518152/

相关文章:

google-apps-script - Google Apps脚本,使用格式将一个电子表格复制到另一电子表格

javascript - 如何确保自动邮件中附加的PDF是最新信息?

javascript - 客户端 successHandler 为空,但服务器日志显示结果

google-apps-script - 获取 Google 表单的最后回复

google-apps-script - Google 表单对下拉选项数量的限制

javascript - 根据 Google 表单提交中同一行中的另一个值更改单元格的值

javascript - 谷歌应用程序脚本,将对象从服务器发送到 HTML 模板和 javascript

google-apps-script - 用于在 Google 表格自动完成下拉列表中显示可选参数的 JSDoc

javascript - Google 脚本将一个单元格写入另一个电子表格中的范围

javascript - 获取Google电子表格中二维矩阵的单元格