简体   繁体   English

Google表格功能部分运行第二次

[英]Google Sheets Function Runs Partially a Second Time

I've been creating inventory spreadsheets to help my dad's employees with year end inventory. 我一直在创建库存电子表格,以帮助我父亲的雇员进行年底库存。 A user filling out inventory will enter data into a few cells in a sheet titled "Form", and will then select "SUBMIT" from a dropdown box located on the form. 用户填写清单后,会将数据输入到标题为“表单”的工作表中的几个单元格中,然后从表单上的下拉框中选择“提交”。 The script then uses the form to make the following changes to a sheet titled "MSPL": 然后,脚本使用该表单对标题为“ MSPL”的工作表进行以下更改:

  • Inserts a new row in a specific location, based on data entered in the form 根据表单中输入的数据,在特定位置插入新行
  • Copies data from the form into the cells of the new row 将数据从表单复制到新行的单元格中

This works 95% of the time. 95%的时间有效。 However, sometimes the script seems to run parts of itself a second time. 但是,有时脚本似乎第二次运行其自身的一部分。 I have watched the spreadsheet changes being made in real time and it seems to: add a new row, copy data into the first 3 cells, create a 2nd row above the 1st one created, copy the entire data into this row. 我已经看到了电子表格的实时更改,这似乎是:添加一个新行,将数据复制到前3个单元格中,在创建的第一个单元上方创建一个第二行,然后将整个数据复制到该行中。 With there being only 1 "insertRowAfter" line in the code, I can't figure out why the code would be inserting two. 由于代码中只有1个“ insertRowAfter”行,因此我无法弄清楚为什么代码会插入两个。

function msplFormSubmit() {

  var form = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MSPL Form");
  var mspl = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MSPL");
  var formWIP = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MSPL WIP Form");
  var msplWIP = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MSPL WIP");
  var Pricing = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Pricing")

  if (form.getRange("E2").getValue() == "SUBMIT") {

    var sectionName = form.getRange("C5").getValue() + '"' + " " + form.getRange("C3").getValue() + " - SUBTOTAL";
    var rowSearch = mspl.getRange("B:B").getValues();
    var rowID = 0
    var unitCost = Pricing.getRange("B3:D").getValues();

    form.getRange("E2").clearContent();
    form.getRange("C10").clearContent();
    form.getRange("B10").setValue("Processing ...")

    for (i = 0; i < mspl.getLastRow(); i ++) {
      if (rowSearch[i] != sectionName) {
        rowID += 1
      } else {
        break;
      }
    }

    mspl.insertRowAfter(rowID)

    rowID += 1 //rowID now points to the newly added row, in order to populate it

    if (mspl.getRange(rowID - 1, 2, 1, 1).getBackground() === "#ffffff") {
      mspl.getRange(rowID, 2, 1, 10).setBackground("#dcdcdc")
    }
    if (mspl.getRange(rowID - 1, 2, 1, 1).getBackground() === "#dcdcdc") {
      mspl.getRange(rowID, 2, 1, 10).setBackground("#ffffff")
    }

    mspl.getRange(rowID, 2, 1, 1).setValue(form.getRange("C2").getValue()); //Tag #
    mspl.getRange(rowID, 3, 1, 1).setValue(form.getRange("C3").getValue()); //Description
    mspl.getRange(rowID, 4, 1, 1).setValue(form.getRange("C4").getValue()); //Quantity
    mspl.getRange(rowID, 5, 1, 1).setValue(form.getRange("C5").getValue()); //Thickness
    mspl.getRange(rowID, 5, 1, 1).setNote(mspl.getRange(rowID-1, 5, 1, 1).getNote()); //Thickness Note
    mspl.getRange(rowID, 6, 1, 1).setValue(form.getRange("C6").getValue()); //Width
    mspl.getRange(rowID, 7, 1, 1).setValue(form.getRange("C7").getValue()); //Length
    mspl.getRange(rowID, 8, 1, 2).setFormulasR1C1(mspl.getRange(rowID-1, 8, 1, 2).getFormulasR1C1()); //Area and Weight Formulas
    mspl.getRange(rowID, 11, 1, 1).setFormulasR1C1(mspl.getRange(rowID-1, 11, 1, 1).getFormulasR1C1()); //Cost Formula

    for (i = 0; i < unitCost.length; i ++) {
      if (unitCost[i][0] == form.getRange("C3").getValue() && unitCost[i][1] == form.getRange("C5").getValue()) {
        mspl.getRange(rowID, 10, 1, 1).setValue(unitCost[i][2]);
        break;
      }
    }

    mspl.getRange(rowID, 10 ,1, 1).setNumberFormat("$0.0000")

    form.getRange("C2:C7").clearContent();
    form.getRange("B10").clearContent();
    form.getRange("C10").setValue("READY");
    }
}

Instead of using data validation together with an edit installable trigger, use a custom menu or a clickable image. 与其将数据验证与可编辑的安装触发器一起使用,不如使用自定义菜单或可点击的图像。 For details see Custom menus in Google Sheets . 有关详细信息,请参见Google表格中的自定义菜单

Another alternative could be to change the value of the cell with the data validation immediately after (form.getRange("E2").getValue() == "SUBMIT") is evaluated / before any other change be made by the script. 另一种选择是在评估(form.getRange("E2").getValue() == "SUBMIT") /在脚本进行任何其他更改之前,立即通过数据验证更改单元格的值。

I noticed that you specified a range in the following way: 我注意到您以以下方式指定了范围:

var unitCost=Pricing.getRange("B3:D").getValues();

I'm thinking it might be that sometimes this range may go well beyond your data so perhaps you can specify it like this: 我想这可能是因为有时这个范围可能超出您的数据范围,所以您可以这样指定:

var unitCost=Pricing.getRange(3,2,Pricing.getLastRow(),4);

Note: I'm not saying that this is the problem I'm just going through your code and seeing how I might write the same thing. 注意:我并不是说这是问题,我只是在检查您的代码并查看如何编写相同的内容。

Note: 注意:

for (i=0;i<unitCost.length;i++){
      if(unitCost[i][0]==form.getRange("C3").getValue() && unitCost[i][1]==form.getRange("C5").getValue()){
        mspl.getRange(rowID, 10).setValue(unitCost[i][2]);
        break;
      }
    }

This code could take a long time to run if you have 1000 rows below the bottom of your data. 如果您的数据底部下方有1000行,则此代码可能需要很长时间才能运行。 in range 'B3:D' ; 范围'B3:D' ;

You only need to get one formula here: 您只需要在这里获得一个公式:

//mspl.getRange(rowID, 11).setFormulasR1C1(mspl.getRange(rowID-1, 11).getFormulasR1C1()); //Cost Formula
    mspl.getRange(rowID, 11).setFormulaR1C1(mspl.getRange(rowID-1,11).getFormulaR1C1()); //Cost Formula

Here's you code after some minor tweaking. 经过一些细微调整后,这是您的代码。 Not sure it's worth anything but I also included a menu function in there that might save you some time. 不确定它是否值得,但是我在其中还包括一个菜单功能,可以节省您一些时间。

//you can change this to opOpen() or connected it up to an onOpen() trigger in the edit menu/current project's triggers.
function runFormSubmitMenu(){ 
  SpreadsheetApp.getUi().createMenu('My Tools')
      .addItem('Submit', 'msplFormSubmit')
      .addToUi();
}


function msplFormSubmit(){
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var form = ss.getSheetByName("MSPL Form");
  var mspl = ss.getSheetByName("MSPL");
  var formWIP = ss.getSheetByName("MSPL WIP Form");
  var msplWIP = ss.getSheetByName("MSPL WIP");
  var Pricing = ss.getSheetByName("Pricing")

  if (form.getRange("E2").getValue()=="SUBMIT"){
    var sectionName=form.getRange("C5").getValue() + '" ' + form.getRange("C3").getValue() + " - SUBTOTAL";
    var rowSearch=mspl.getRange("B:B").getValues();
    var rowID=0;
    //var unitCost=Pricing.getRange("B3:D").getValues();
    var unitCost=Pricing.getRange(3,2,Pricing.getLastRow(),4);
    form.getRange("E2").clearContent();
    form.getRange("C10").clearContent();
    form.getRange("B10").setValue("Processing ...");

    for(var i=0;i<mspl.getLastRow();i++){
      if(rowSearch[i]!=sectionName){
        rowID++;
      } else {
        break;
      }
    }
    mspl.insertRowAfter(rowID);
    rowID++; 
    if (mspl.getRange(rowID - 1, 2).getBackground()=="#ffffff"){
      mspl.getRange(rowID, 2, 1, 10).setBackground("#dcdcdc");
    }
    if (mspl.getRange(rowID - 1, 2).getBackground()==="#dcdcdc"){
      mspl.getRange(rowID, 2, 1, 10).setBackground("#ffffff");
    }
    mspl.getRange(rowID, 2).setValue(form.getRange("C2").getValue()); //Tag #
    mspl.getRange(rowID, 3).setValue(form.getRange("C3").getValue()); //Description
    mspl.getRange(rowID, 4).setValue(form.getRange("C4").getValue()); //Quantity
    mspl.getRange(rowID, 5).setValue(form.getRange("C5").getValue()); //Thickness
    mspl.getRange(rowID, 5).setNote(mspl.getRange(rowID-1, 5, 1, 1).getNote()); //Thickness Note
    mspl.getRange(rowID, 6).setValue(form.getRange("C6").getValue()); //Width
    mspl.getRange(rowID, 7).setValue(form.getRange("C7").getValue()); //Length
    mspl.getRange(rowID, 8, 1, 2).setFormulasR1C1(mspl.getRange(rowID-1, 8, 1, 2).getFormulasR1C1()); //Area and Weight Formulas
    mspl.getRange(rowID, 11).setFormulaR1C1(mspl.getRange(rowID-1,11).getFormulaR1C1()); //Cost Formula


    for (i=0;i<unitCost.length;i++){
      if(unitCost[i][0]==form.getRange("C3").getValue() && unitCost[i][1]==form.getRange("C5").getValue()){
        mspl.getRange(rowID, 10).setValue(unitCost[i][2]);
        break;
      }
    }
    mspl.getRange(rowID,10).setNumberFormat("$0.0000");
    form.getRange("C2:C7").clearContent();
    form.getRange("B10").clearContent();
    form.getRange("C10").setValue("READY");
    }
}

You'll probably have to figure this one out on your own. 您可能必须自己弄清楚这一点。 Trying using the Logger.log() function. 尝试使用Logger.log()函数。 It's pretty useful and take a look at the execution logs. 这非常有用,并查看执行日志。

If this isn't particularly useful to you leave me a comment and I'll just delete it because I think people are more inclined to answer questions that don't have many answers. 如果这对您不是特别有用,请给我留言,然后删除它,因为我认为人们更倾向于回答没有很多答案的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM