【问题标题】:overwrite/edit specific records on an existing sheet with vlookup on save dialog使用保存对话框上的 vlookup 覆盖/编辑现有工作表上的特定记录
【发布时间】:2016-10-03 20:50:26
【问题描述】:

我有一个脚本,可以将数据从我称为“表单”的一张表保存到另一张我称为“记录”的表中。当我单击工作簿中设置为按钮的图像时,该函数运行。

function SaveEWSReport() {
//Save and clear the form
  var ss2 = SpreadsheetApp.getActive();
  var source = ss2.getSheetByName('Form');
  var records = ss2.getSheetByName(source.getRange('A1').getValues()); //get the tab to send the save data to.
  var val = source.getRange('A20:Q20').getValues(); //get the cells with information to copy (contain information concatenated form dropdown cells in B2 to P19) 
  // get values from zero indext cells and save to records. 
  // 0,0=StudentB3.0,1=GradeD2.0,2=Date.0,2=FlagC5.0,3=ReferedByD5.7,0=NotesB9:F15.3,0=TeirB5.5,0=TypeB7. Nulls leave spaces. 
  var write = [val[0][0], val[0][1],null, val[0][2], val[0][3], null, null, val[0][4], val[0][5], val[0][6], val[0][7],null];            
  records.appendRow(write);  

  //Clear the cells for the next use.
  source.getRange('D2').clearContent();
  source.getRange('B3').clearContent();
  source.getRange('B3:D3').mergeAcross(); // this merges the cell to self heal potential user error.
  source.getRange('B5').clearContent();
  source.getRange('D5').clearContent();
  source.getRange('B7').clearContent();
  source.getRange('B7:F7').mergeAcross(); // this merges the cell to self heal potential user error.
  source.getRange('B9').clearContent();
  source.getRange('B9:F15').merge(); // this merges the cell to self heal potential user error.

问题是我将数据追加到新行

 records.appendrow(write)

但是,我希望能够覆盖现有行。 例如,如果记录表上写着:

A3          B3        C3
Student 1 , Grade 6 , Note Set  
Student 2 , Grade 8 , Note set
Student 3 , Grade 8 , Note set
A7          B7        C7

然后我为学生 2 添加一个新笔记,然后保存。我想看到同样的东西,但它附加了一行,所以它看起来像这样:

A3          B3        C3
Student 1 , Grade 6 , Note Set 
Student 2 , Grade 8 , Note set
Student 3 , Grade 8 , Note set
Student 2 , Grade 8 , Note set
A8          B8        C8

所以你看我需要找到一种方法让脚本进行 vlookup 并找到学生 2,以便它可以将新数据写入同一行,但我不知道如何让脚本写入该行而不是而不是追加一个新的。

我希望这是有道理的。我不经常编程,所以我不确定我的措辞是否正确,我已经搜索过解决方案,但我可能在错误的领域进行搜索,因为我有限的知识和经验。如果这需要澄清,请随时询问。

这是一个包含完整脚本的虚拟表格的链接。 https://docs.google.com/spreadsheets/d/1J2rCtSmt_BM6CozO4EBdlj1YL2wtoW4D4gNCsbalO5M/edit?usp=sharing

【问题讨论】:

    标签: google-apps-script google-sheets saving-data


    【解决方案1】:

    切换下面的append row语句,它会查看是否能在Records表的第一列找到名字,如果找到则覆盖,否则追加。

    var recordData = records.getRange(3, 1, records.getLastRow(), 1).getValues();
    var recordPosition = recordData.map(function(row) {return row[0];}).indexOf(val[0][0]); 
    
    if (recordPosition === -1) {
      records.appendRow(write);  
    } else {
      records.getRange(3 + recordPosition, 1, 1, write.length).setValues([write]);
    }
    

    如果您根本不想追加,可以将其简化为:

    var recordPosition = records
        .getRange(3, 1, records.getLastRow(), 1) 
        .getValues()
        .map(function(row) {return row[0];})
        .indexOf(val[0][0]); 
    
    records.getRange(3 + recordPosition, 1, 1, write.length).setValues([write]);
    

    【讨论】:

    • 太棒了!谢谢!
    【解决方案2】:
    function SaveEWSReport() {
    //Save and clear the form
      var ss2 = SpreadsheetApp.getActive();
      var formSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form');//++++
    
      var recordSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Records');//++++
      var recordData = recordSheet.getDataRange().getValues();//++++
      var source = ss2.getSheetByName('Form');
      var records = ss2.getSheetByName(source.getRange('A1').getValues()); //get the tab to send the save data to.
      var val = source.getRange('A20:Q20').getValues(); //get the cells with information to copy (contain information concatenated form dropdown cells in B2 to P19) 
      // get values from zero indext cells and save to records. 
      // 0,0=StudentB3.0,1=GradeD2.0,2=Date.0,2=FlagC5.0,3=ReferedByD5.7,0=NotesB9:F15.3,0=TeirB5.5,0=TypeB7. Nulls leave spaces. 
      var write = [val[0][0], val[0][1],null, val[0][2], val[0][3], null, null, val[0][4], val[0][5], val[0][6], val[0][7],null];            
      //records.appendRow(write); 
      var flag = 0;
      for(var i = 0; i < recordData.length; i++)
      {
        if(recordData[i][0] == val[0][0])
        {
          flag = 1;
          break;
        }
      }
    
      if(flag == 1)
      {
        for(var i = 0; i < recordData.length; i++)
        {
          if(recordData[i][0] == val[0][0])
          {
            for( var j = 1; j <= 12; j++)
            {
              recordSheet.getRange(i+1, j).setValue(val[0][j]);
            }
          }
        }
      }
    
      else
        records.appendRow(write);
    
      //Clear the cells for the next use.
      source.getRange('D2').clearContent();
      source.getRange('B3').clearContent();
      source.getRange('B3:D3').mergeAcross(); // this merges the cell to self heal potential user error.
      source.getRange('B5').clearContent();
      source.getRange('D5').clearContent();
      source.getRange('B7').clearContent();
      source.getRange('B7:F7').mergeAcross(); // this merges the cell to self heal potential user error.
      source.getRange('B9').clearContent();
      source.getRange('B9:F15').merge(); // this merges the cell to self heal potential user error.
    }
    

    因此,您还可以使用简单的 for 循环来搜索特定工作表中的特定单元格值。

    【讨论】:

    • 谢谢。这很有帮助。我今天将使用 Gertenbachs 先生的解决方案,但这是很好的信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2020-09-07
    • 2021-09-17
    相关资源
    最近更新 更多