Google电子表格脚本无法独立于其他脚本运行

时间:2017-03-09 22:26:22

标签: javascript user-interface google-apps-script google-sheets spreadsheet

我已经设置了一个粗略的脚本(对此更新),以自动执行一些我必须始终如一的数据输入。我使用复选框构建一个UI,并根据对检查的响应将数据填入工作表。这一切都适用于一种产品。问题是我已经为其他产品复制了这个。为第二个产品重新创建时,我必须将第二个脚本中的函数重命名为“BUILDUI2”,以便查看正确的选项显示(否则它将显示上一个脚本中的选项)。然后,即使我看到正确的复选框,对复选框的响应也基于创建的第一个脚本。因此,在第二个产品上,选择第二个复选框会插入原始BuildUI脚本中指定的数据,该脚本与另一个产品相关联,而不是BuildUI2下显示的数据。这些是由工作表上单独的按钮激活的独立脚本。有关如何让第二个(以及第三个和第四个)脚本读取正确信息的任何见解?

产品1脚本:

  function BuildUI() {
  //create the application itself
  var app = UiApp.createApplication().setHeight(160).setWidth(250);
  app.setTitle("Options");

  //create panels and add them to the UI
  var panel = app.createVerticalPanel();

  //create a text box
  var check1 = app.createCheckBox("Safety Aprons");
  check1.setName('CheckBox1');

  var check2 = app.createCheckBox("Standard Trash Racks (1)");
  check2.setName('CheckBox2');

  var check3 = app.createCheckBox("Standard Trash Racks (2) ");
  check3.setName('CheckBox3');

  var check4 = app.createCheckBox("Plate Style Trash Racks (1)");
  check4.setName('CheckBox4');

  var check5 = app.createCheckBox("Plate Style Trash Racks (2) ");
  check5.setName('CheckBox5');   

  var check6 = app.createCheckBox("6:1 Safety Grates (1)");
  check6.setName('CheckBox6');

  var check7 = app.createCheckBox("6:1 Safety Grates (2)");
  check7.setName('CheckBox7');

  //create a submit button
  var button = app.createButton('Done');

  //add the text box and the button to the panel
  panel.add(check1);
  panel.add(check2);
  panel.add(check3);
  panel.add(check4);
  panel.add(check5); 
  panel.add(check6);
  panel.add(check7);
  panel.add(button);

  var handler = app.createServerHandler("submitButton");
  button.addClickHandler(handler);
  handler.addCallbackElement(panel); 

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

  var doc = SpreadsheetApp.getActive();
  doc.show(app);
  }

  function submitButton(e){
  var app = UiApp.getActiveApplication();
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastrow = sheet.getLastRow();

  if(e.parameter.CheckBox1 == 'true') {


  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  var rowsDeleted = 0;
  for (var i = lastrow-4; i <= numRows - 1; i++) {
  var row = values[i];
  if (row[2] == 'R012M-BAM' || row[2] == 'R012M-BAF') {
  sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
  rowsDeleted++;}
  } 
  var lastrow = sheet.getLastRow();

  sheet.getRange(lastrow+1,3).setValue("R012M-BSAF6");
  sheet.getRange(lastrow+1,5).setValue("1");
  sheet.getRange(lastrow+2,3).setValue("R012M-BSAM6");
  sheet.getRange(lastrow+2,5).setValue("1");
  }

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox2 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00092");
   sheet.getRange(lastrow+1,5).setValue("1");
  } 

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox3 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00092");
   sheet.getRange(lastrow+1,5).setValue("2");
  }     

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox4 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00094");
   sheet.getRange(lastrow+1,5).setValue("1");
  } 

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox5 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00094");
   sheet.getRange(lastrow+1,5).setValue("2");
  }     

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox6 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00095");
   sheet.getRange(lastrow+1,5).setValue("1");
  }  

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox7 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00095");
   sheet.getRange(lastrow+1,5).setValue("2");
  }    

  return app.close();
  }

产品2脚本:

  function BuildUI2() {
  //create the application itself
  var app = UiApp.createApplication().setHeight(225).setWidth(250);
  app.setTitle("Options");

  //create panels and add them to the UI
  var panel = app.createVerticalPanel();

  //create a text box
  var check1 = app.createCheckBox("Safety Aprons");
  check1.setName('CheckBox1');

  var check2 = app.createCheckBox("Pipe Style Trash Racks (1)");
  check2.setName('CheckBox2');

  var check3 = app.createCheckBox("Pipe Style Trash Racks (2) ");
  check3.setName('CheckBox3');

  var check4 = app.createCheckBox("Plate Style Trash Racks (1)");
  check4.setName('CheckBox4');

  var check5 = app.createCheckBox("Plate Style Trash Racks (2) ");
  check5.setName('CheckBox5');

  var check6 = app.createCheckBox("Bull Nose Style Trash Racks (1)");
  check6.setName('CheckBox6');

  var check7 = app.createCheckBox("Bull Nose Style Trash Racks (2) ");
  check7.setName('CheckBox7');

  var check8 = app.createCheckBox("6:1 Safety Grates (1)");
  check8.setName('CheckBox8');

  var check9 = app.createCheckBox("6:1 Safety Grates (2)");
  check9.setName('CheckBox9');

  //create a submit button
  var button = app.createButton('Done');

  //add the text box and the button to the panel
  panel.add(check1);
  panel.add(check2);
  panel.add(check3);
  panel.add(check4);
  panel.add(check5); 
  panel.add(check6);
  panel.add(check7);
  panel.add(check8);
  panel.add(check9);
  panel.add(button);

  var handler = app.createServerHandler("submitButton");
  button.addClickHandler(handler);
  handler.addCallbackElement(panel); 

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

  var doc = SpreadsheetApp.getActive();
  doc.show(app);
  }

  function submitButton(e){
  var app = UiApp.getActiveApplication();
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastrow = sheet.getLastRow();

  if(e.parameter.CheckBox1 == 'true') {


  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  var rowsDeleted = 0;
  for (var i = lastrow-4; i <= numRows - 1; i++) {
  var row = values[i];
  if (row[2] == 'R015M-BAM' || row[2] == 'R015M-BAF') {
  sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
  rowsDeleted++;}
  } 
  var lastrow = sheet.getLastRow();

  sheet.getRange(lastrow+1,3).setValue("R015M-BSAF6");
  sheet.getRange(lastrow+1,5).setValue("1");
  sheet.getRange(lastrow+2,3).setValue("R015M-BSAM6");
  sheet.getRange(lastrow+2,5).setValue("1");
  }

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox2 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00194");
   sheet.getRange(lastrow+1,5).setValue("1");
  } 

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox3 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00194");
   sheet.getRange(lastrow+1,5).setValue("2");
  }     

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox4 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00097");
   sheet.getRange(lastrow+1,5).setValue("1");
  } 

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox5 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00097");
   sheet.getRange(lastrow+1,5).setValue("2");
  }  

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox6 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00440");
   sheet.getRange(lastrow+1,5).setValue("1");
  } 

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox7 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00440");
   sheet.getRange(lastrow+1,5).setValue("2");
  }  

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox8 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00101");
   sheet.getRange(lastrow+1,5).setValue("1");
  }  

  var lastrow = sheet.getLastRow();
  if(e.parameter.CheckBox9 == 'true') 
  {sheet.getRange(lastrow+1,3).setValue("85-00101");
   sheet.getRange(lastrow+1,5).setValue("2");
  }    

  return app.close();
  }

0 个答案:

没有答案