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

时间:2016-10-03 20:50:27

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

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

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并找到student 2,这样它就可以在同一行写入新数据而我不知道如何让脚本写入该行而不是而不是添加一个新的。

我希望这是有道理的。我不经常编程,所以我不确定我是否正确写了这个,我搜索了解决方案,但我可能会在错误的领域中搜索我有限的知识和经验。如果需要澄清,请不要犹豫。

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

2 个答案:

答案 0 :(得分:0)

切换下面的附加行语句,它将查看它是否可以在记录表的第一列中找到该名称,如果是,则覆盖,否则附加。

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]);

答案 1 :(得分:0)

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循环来搜索特定工作表中的特定单元格值。