更新列表框选择的索引

时间:2015-03-20 16:55:29

标签: javascript google-apps-script listbox google-form listboxitems

我有一个谷歌脚本,可以在谷歌电子表格和谷歌表格之间进行读写。它基本上是访问材料数据库的一种形式。

首次访问表单时,它会显示所有空白字段,填充材料ID(CW_ID)的“选择器”列表框以及选择器列表框中“新建”部分的选项。如果用户保持“新建”状态,则字段保持空白,以便用户手动填充它们并将其作为数据库中的新项目附加。如果用户滚动“选择器”列表框并选择CW_ID,它将从电子表格中提取并使用与所选CW_ID对应的数据填充字段,供用户进行编辑(修改部件的详细信息)。

因为我在数据库中使用SQL结构,所以我有几个链接表:

  • 材料表(包含有关零件的大部分信息,包括制造商[制造商_ID]的ID)

  • 制造商表(链接到materials.manufacturer_ID)

目前,表单仅使用材料表填充字段,因此在“制造商”字段中,它仅显示ID号。出于用户目的,我希望脚本检查材料表中显示的manufacturer_ID,进入制造商表,查找匹配项并让制造商列表框将其显示为当前选定的索引。 (或者只是让制造商的名字出现在现场)

到目前为止,这是我脚本的一部分:

(滚动到底部“// ****这是我工作的部分”)

function doGet() {
  var ss = SpreadsheetApp.openByUrl("https://docs.google.com/something")
  var materialsSheet = ss.getSheetByName('materials');
  var manufacturersSheet = ss.getSheetByName('manufacturers');
  var vendorsSheet = ss.getSheetByName('vendors');
  var usersSheet = ss.getSheetByName('users');
  var projectApp = UiApp.createApplication();
  projectApp.setTitle("Materials Form");
  var activeEmail = Session.getActiveUser().getEmail();

  //I create the vertical panel.
  var panel = projectApp.createVerticalPanel().setId('face'); 


  //Here is where I actually create the drop down menu, and assign the function "goSelection" to be activated whenever a selection is made.
  var selector = projectApp.createListBox(true);
  selector.setName('selectionBox').setId('selectionBox').addItem('New');
  var materialsData = materialsSheet.getDataRange().getValues();
  //do until row is less than length of 
  for (var i=0; i < materialsData.length; i++){ 
    if (materialsData [i][1] == "Email"){
      //if row in Column B does not equal gmail, skip to i++
      continue;
    }
    //add Column C (CW_ID) of current row to the selector list
    selector.addItem(materialsData [i][2]); 
  }
  selector.setSelectedIndex(0);
  var selectHandler = projectApp.createServerHandler('goSelection');
  selectHandler.addCallbackElement(panel);
  selector.addChangeHandler(selectHandler);



 //Here is where I create the drop down menu to show list of manufacturers from manufacturers sheet
  var manufSelectorLabel = projectApp.createHTML("<br><b>Manufacturer</b><br>").setWidth('100%');
  var manufSelector = projectApp.createListBox(true);
  manufSelector.setName('manufSelectionBox').setId('manufSelectionBox').addItem('New');
  var manufacturersData = manufacturersSheet.getDataRange().getValues();
  //do until row is less than length of 
  for (var i=0; i < manufacturersData.length; i++){ 
    if (manufacturersData [i][1] == "Email"){
      //if row in Column B does not equal gmail, skip to i++
      continue;
    }
    //add Column C (Manufacturers) of current row to the selector list
    manufSelector.addItem(manufacturersData [i][3]); 
  }
  manufSelector.setSelectedIndex(0);
  var manufSelected = manufSelector.SelectedItem;'
  //**I am unsure whether this should have it's own function
  //var manufSelectHandler = projectApp.createServerChangeHandler('goManuf');
  //manufSelectHandler.addCallbackElement(panel);
  //manufSelector.addChangeHandler(manufSelectHandler);


  var gmailLabel = projectApp.createHTML("<br><b>Gmail:</b><br>").setWidth('100%');
  var gmailField = projectApp.createTextArea().setSize('100%', '25px');
  gmailField.setName('gmailArea').setId('gmailArea');
  gmailField.setText(activeEmail);

  var savedLabel = projectApp.createLabel('Thank you for your submission.');
  savedLabel.setVisible(false).setId('sLabel');

  //At this point, I'm actually declaring the variables for all the fields and text for the actual form. 
  var selectorLabel = projectApp.createHTML("<br><b>Select CW_ID from list.</b>").setId('selectLabel');  


  var descriptionLabel = projectApp.createHTML("<br><b>Description</b><br>").setWidth('100%');
  var descriptionField = projectApp.createTextArea().setSize('100%', '100px');
  descriptionField.setName('descriptionArea').setId('descriptionArea');    

  var manufacturerLabel = projectApp.createHTML("<br><b>Manufacturer</b></br>").setWidth('100%');
  var manufacturerField = projectApp.createTextArea().setSize('100%x', '25px');
  manufacturerField.setName('manufacturerArea').setId('manufacturerArea');    

  var manufacturerListLabel = projectApp.createHTML("<br><b>ManufacturerList</b></br>").setWidth('100%');
  var manufacturerListField = projectApp.createTextArea().setSize('100%x', '25px');
  manufacturerListField.setName('manufacturerListArea').setId('manufacturerListArea');

  var modelnumberLabel = projectApp.createHTML("<br><b>Model Number</b><br>").setWidth('100%');
  var modelnumberField = projectApp.createTextArea().setSize('100%', '25px');
  modelnumberField.setName('modelnumberArea').setId('modelnumberArea');   


  //Next, i create the save button and assign the function "saved" to be activated whenever the button is pressed.
  var saveButton = projectApp.createButton('Save');
  var saveHandler = projectApp.createServerHandler('saved');
  saveHandler.addCallbackElement(panel);
  saveButton.addClickHandler(saveHandler);

  //Now that all the componentes of the form have been declared and set up, I'm going to assemble them on the panel.
  panel.setSpacing(6);
  panel.add(nameLabel);
  panel.add(nameField);
  panel.add(gmailLabel);
  panel.add(gmailField);  
  panel.add(selectorLabel);
  panel.add(selector);
  panel.add(descriptionLabel);
  panel.add(descriptionField);
  panel.add(manufSelectorLabel);
  panel.add(manufSelector);
  panel.add(manufacturerLabel);
  panel.add(manufacturerField);
  panel.add(modelnumberLabel);
  panel.add(modelnumberField);
  panel.add(saveButton);
  panel.add(savedLabel);
  projectApp.add(panel);
  return projectApp;
}


  //This function looks to see what has been selected in the drop down menu, and then pulls the appropriate data from the spreadsheet to display in the fields.
   function goSelection(e){
  var activeEmail = Session.getActiveUser().getEmail();
  var app = UiApp.getActiveApplication();
  var gmailField = app.getElementById('gmailArea');
  var nameField = app.getElementById('nameArea');
  var chosen = e.parameter.selectionBox;
  var ss = SpreadsheetApp.openByUrl("https://docs.google.com/something")

  var materialsSheet = ss.getSheetByName('materials');
  var manufacturersSheet = ss.getSheetByName('manufacturers');
  var vendorsSheet = ss.getSheetByName('vendors');
  var materialsData = materialsSheet.getDataRange().getValues();
  var manufacturersData = manufacturersSheet.getDataRange().getValues();
  var vendorsData = vendorsSheet.getDataRange().getValues();

  var panel = app.getElementById('face');
  var standardpartField = app.getElementById('standardpartArea');
  var descriptionField = app.getElementById('descriptionArea');
  var manufacturerField = app.getElementById('manufacturerArea');
  var manufacturerListField = app.getElementById('manufSelectionBox');
  var modelnumberField = app.getElementById('modelnumberArea');

if (chosen != 'New') {
 for (var i=1; i < materialsData.length; i++){
    if (materialsData [i][1] == "Email"){
  //if row in Column B does not equal gmail, skip to i++
  continue;
   }
   if (materialsData [i][2] != chosen){
    continue;
    }
    nameField.setText(materialsData [i][0]);
    gmailField.setText(materialsData [i][1]);
    standardpartField.setText(materialsData [i][3]);
    descriptionField.setText(materialsData [i][4]);


    //****THIS IS THE PART I'M WORKING ON
    //set manufacturerField to manufacturer of current row
    //loop through manufacturer sheet until row matches with manufacturersField
    //when a match is found, selector box index to same row

    manufacturerField.setText(materialsData [i][5]);      

    for (var i=1; i < manufacturersData.length; i++){
      if (manufacturersData [i][1] == "Email"){
      //if row in Column B does not equal Email, skip to i++
        continue;
      }
      if (manufacturersData [i][2] != manufacturerField){
        continue;
      }
     manufacturerListField.setSelectedIndex(i);
    }

    modelnumberField.setText(materialsData [i][6]);
    }
}

这对我有意义,但它不起作用。我点击选择器列表框中的一个部分,所有信息都填充到字段中,正如我想要的那样。但是,制造商列表框不会跳转到正确的制造商(如制造商字段中所填充的那样),它只是坐在那里并按原样保留。

(见图片以了解表格的外观)

http://oi61.tinypic.com/6h6tqp.jpg

制造商列表框和字段显示,但是现在,只有制造商字段更改才能在选择部件时显示数据。

任何帮助都会非常感激! :)

1 个答案:

答案 0 :(得分:0)

以下是一些可以帮助您开始使用HTML服务的HTML:

输入表格HTML

<div>
  <div>
      Name:
     <select>
      <option value="one">One</option>
      <option value="two">Two</option>
      <option value="three">Three</option>
      <option value="four">Four</option>
    </select>
  </div>

  <br/>

  <div>
    Gmail:
     <select>
      <option value="one">One</option>
      <option value="two">Two</option>
      <option value="three">Three</option>
      <option value="four">Four</option>
    </select>
  </div>

  <br/>

  <div>Select_CW_ID_From List:</div>
    <select name="CW_ID" multiple>
    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
    <option value="four">Four</option>
    </select>
  </div>

  <br/>

  <div>Standard Part</div>

  <input type="text"/>

  <br/>
  <br/>
  <div>Description</div>
  <textarea rows="4" cols="50" name="comment" form="usrform">
Enter text here...</textarea>
</div>

<script>
  function onSuccess(argReturnValue) {
    alert('was successful ' + argReturnValue);
  }

  google.script.run.withSuccessHandler(onSuccess)
      .nameOfFunctionInGS_File();
</script>

下载Notepad ++:Notepad plus plus

设计你的HTML。然后在Apps脚本项目中创建一个HTML文件。

您的doGet()应该只有提供HTML的代码。

Code.gs

function doGet() {

  return HtmlService.createTemplateFromFile('myHTML_File_Name_Here')
    .evaluate() // evaluate MUST come before setting the NATIVE mode
    .setTitle('Materials Form')
    .setSandboxMode(HtmlService.SandboxMode.NATIVE);
};

创建另一个.gs文件来处理数据。这就是用于将数据写入电子表格的所有代码都将用于的地方。

如果您需要填充选择框,可以在HTML的脚本标记中使用JavaScript。

通过我给你的,你应该能够创建一个独立的应用程序脚本HTML服务应用程序。发布它,然后运行它。你可能用你当前的项目做到了。