使用保存时的vlookup对话框覆盖/编辑现有工作表上的特定记录



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

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

将append row语句切换为以下语句,它将查看是否能在记录表的第一列中找到名称,如果能找到,则覆盖,否则追加。

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]);
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循环来搜索特定图纸中的特定单元格值。

最新更新