谷歌应用程序脚本错误:索引(第7行,第0列)超出范围

时间:2015-01-30 20:00:43

标签: google-apps-script

GAS中的Noob项目,当我发布到网络应用程序并查看它会产生上述错误。 “索引(第7行,第0列)超出界限”我从未见过这种情况,我看起来和其他人在执行复杂的数组函数时遇到了问题。但无论如何,这个小家伙相当简单。难倒!     你能给我指点正确方向的任何帮助都会很棒。

var submissionSSKey = '1Ccj_g3HtkHOcdAtn_9LHUxUrbsh5ZJYVUs3hWC9-Cuo';
var docurl = 'https://docs.google.com/spreadsheets/d/1Ccj_g3HtkHOcdAtn_9LHUxUrbsh5ZJYVUs3hWC9-Cuo/edit'
var LOCATION = ['Select a Location','A Res','B Res','C Res','D Res','MNT','OSB','TWB','VP','VM','SITE DEV']
var QCREP = ['Select a REP','David','Ryan','Herschell','Bill','Nick ','Stephe','Beth ']
var Shop = ['Select a SHOP','Walls/Ceilings','Plumbing','Electrical','HVAC/Sheet Metal','Carpentry','Interiors','Exteriors','Masonry','Mechanics','Structural']
var ISSUETYPE = ['Select an ISSUE TYPE','Poor Workmanship','Lack of Training','Not Per Plans','Not Per Code','Not Per Spec','Plan Conflict']
var Panelstyle = {'background':'#dddddd','padding':'40px','borderStyle':'solid','borderWidth':'10PX','borderColor':'#bbbbbb'}

function doGet() {
  var app = UiApp.createApplication().setTitle('QC Observation').setStyleAttribute('padding','50PX');
  var panel = app.createFormPanel().setStyleAttributes(Panelstyle).setPixelSize(400, 600);
  var title = app.createHTML('<B>QC Observation</B>').setStyleAttribute('color','grey').setStyleAttribute('fontSize','25PX');
  var grid = app.createGrid(7,2).setId('grid');
  var location = app.createListBox().setName('location').setWidth('130');
   for(var i in LOCATION){location.addItem(LOCATION[i])}    
  var QCREP = app.createListBox().setName('QCREP').setWidth('130');
   for(var i in QCREP){QCREP.addItem(QCREP[i])}  
  var Shop = app.createListBox().setName('Shop').setWidth('130');
   for(var i in Shop){Shop.addItem(Shop[i])}  
  var ISSUETYPE = app.createListBox().setName('ISSUETYPE').setWidth('130');
   for(var i in ISSUETYPE){ISSUETYPE.addItem(ISSUETYPE[i])}    
  var Textbox1 = app.createTextBox().setWidth('200px').setName('TB1');
  var OBDate = app.createDateBox().setWidth('150px').setName('OBdate');
  var FOLLOWDate = app.createDateBox().setWidth('150px').setName('FOLLOwDate');
  var upLoad = app.createFileUpload().setName('uploadedFile');
  var upLoad1 = app.createFileUpload().setName('uploadedFile1');
  var submitButton = app.createSubmitButton('<B>Submit</B>'); 
  var warning = app.createHTML('Please fill in all fields').setStyleAttribute('background','#bbbbbb').setStyleAttribute('fontSize','18px');
  //file upload
  var cliHandler2 = app.createClientHandler()
  .validateLength(Textbox1, 1, 40).validateNotMatches(location,'Select a LOCATION').validateNotMatches(upLoad, 'FileUpload');


  var cliHandler3 = app.createClientHandler()
  .validateNotMatches(upLoad1, 'FileUpload').forTargets(submitButton).setEnabled(true)
  .forTargets(warning).setHTML('Now you can submit your form').setStyleAttribute('background','#99FF99').setStyleAttribute('fontsize','12px');

  var cliHandler4 = app.createClientHandler()
  .validateLength(Textbox1, 1, 250).validateNotMatches(location,'Select a LOCATION').validateNotMatches(upLoad, 'FileUpload');

  //Grid layout of items on form
  grid.setWidget(0, 1, title)
      .setText(1, 0, 'LOCATION')
      .setWidget(1, 1, location.addClickHandler(cliHandler2))
      .setText(2, 0, 'QC REP')
      .setWidget(2, 1, QCREP.addClickHandler(cliHandler2))
      .setText(3, 0, 'SHOP RESPONSIBLE')
      .setWidget(3, 1, Shop.addClickHandler(cliHandler2))
      .setText(4, 0, 'ISSUE TYPE')
      .setWidget(4, 1, ISSUETYPE.addClickHandler(cliHandler2))
      .setText(5, 0, 'OBSERVATION')
      .setWidget(5, 1, Textbox1.addClickHandler(cliHandler4))
      .setText(6, 0, 'OBSERVATION DATE')
      .setWidget(6, 1, OBDate.addValueChangeHandler(cliHandler2))
      .setText(7, 0, 'FOLLOW UP DATE')
      .setWidget(7, 1, FOLLOWDate.addValueChangeHandler(cliHandler2))
      .setText(8, 0, 'IMAGE FILE')
      .setWidget(8, 1, upLoad.addChangeHandler(cliHandler2))
      .setWidget(9, 1, upLoad1.addChangeHandler(cliHandler3))
      .setWidget(11, 0, submitButton)
      .setWidget(11, 1, warning);

  var cliHandler = app.createClientHandler().forTargets(warning).setHTML('<B>PLEASE WAIT WHILE THE FILE IS UPLOADING<B>').setStyleAttribute('background','orange');
  submitButton.addClickHandler(cliHandler).setEnabled(false);  
  panel.add(grid);
  app.add(panel);
  return app;
}


function doPost(e) {
  Logger.log('Value of e is: ' + e);
  var app = UiApp.getActiveApplication();
  var ListVal = e.parameter.location;
  var ListVal = e.parameter.QCREP;
  var ListVal = e.parameter.Shop;
  var ListVal = e.parameter.ISSUETYPE;
  var textVal = e.parameter.TB1;
  var dateVal = e.parameter.OBDate;
  var dateVal2 = e.parameter.FOLLOWDate;
  var fileBlob1 = e.parameter.uploadedFile;
  var blob1 = fileBlob1.setContentTypeFromExtension()
  var img1 = DocsList.createFile(blob1);
  var fileBlob2 = e.parameter.uploadedFile1;
  var blob2 = fileBlob2.setContentTypeFromExtension()
  var img2 = DocsList.createFile(blob2);
  try{
  var folder = DocsList.getFolder('photos');
  }catch(e){DocsList.createFolder('photos');var folder = DocsList.getFolder('photos')}
  img1.addToFolder(folder);
  img1.removeFromFolder(DocsList.getRootFolder());
  img2.addToFolder(folder);
  img2.removeFromFolder(DocsList.getRootFolder());
  var weight1 = parseInt(img1.getSize()/1000);
  var weight2 = parseInt(img2.getSize()/1000);
  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheetByName('Sheet1');
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 5).setValues([[ListVal,textVal,dateVal,"https://drive.google.com/uc?export=view&id="+img1.getId(),"https://drive.google.com/uc?export=view&id="+img2.getId()]]);
  var image1Insert = sheet.getRange(lastRow+1, 6).setFormula('=image("https://drive.google.com/uc?export=view&id='+img1.getId()+'")');
  var image2Insert = sheet.getRange(lastRow+1, 7).setFormula('=image("https://drive.google.com/uc?export=view&id='+img2.getId()+'")');
  sheet.setRowHeight(lastRow+1, 80);
  var GDoc = DocumentApp.openByUrl(docurl)
  GDoc.appendTable([['LOCATION : '+ListVal,'QCREP : '+ListVal,'SHOP : '+ListVal,'ISSUE TYPE : '+ListVal,'OBSERVATION : '+textVal,'OBDate : '+dateVal,'FOLLOWDate : '+dateVal2]])
  var inlineI = GDoc.appendImage(img1);
  var width = inlineI.getWidth();
  var newW = width;
  var height = inlineI.getHeight();
  var newH = height;
  var ratio = width/height;
  Logger.log('w='+width+'h='+height+' ratio='+ratio);
  if(width>640){
  newW = 640;
  newH = parseInt(newW/ratio);
  }
  inlineI.setWidth(newW).setHeight(newH)
  GDoc.appendParagraph('IMAGE size : '+width+' x '+height+' (eventually) resized to '+newW+' x '+newH+' for PREVIEW ('+weight1+' kB)   ');
  var inlineI = GDoc.appendImage(img2);
  var width = inlineI.getWidth();
  var newW = width;
  var height = inlineI.getHeight();
  var newH = height;
  var ratio = width/height;
  Logger.log('w='+width+'h='+height+' ratio='+ratio);
  if(width>640){
  newW = 640;
  newH = parseInt(newW/ratio);
  }
  inlineI.setWidth(newW).setHeight(newH)
  GDoc.appendParagraph('IMAGE size : '+width+' x '+height+' (eventually) resized to '+newW+' x '+newH+' for PREVIEW ('+weight2+' kB)   ');
  GDoc.appendHorizontalRule();
  GDoc.saveAndClose();
  app.add(app.createLabel('Thank you for submitting'));
  return app
}

1 个答案:

答案 0 :(得分:0)

这是因为这一行:

var grid = app.createGrid(7,2).setId('grid');

网格中的行为零索引。第一行是索引零。第七行是索引6.您正在创建七行,这意味着您只能通过索引引用一行直到数字6.

UI Grid Documentation

当第55行的这行代码运行时:

.setText(7, 0, 'FOLLOW UP DATE')

它指的是第八行,它并不存在。您无法将文本项设置为不存在的行,它超出范围&#34;

您需要将网格更改为12行:

var grid = app.createGrid(12,2).setId('grid');

Ui服务已弃用。您将大量工作投入到将来可能不会支持的工作中。