检索多行

Retrieve multiple rows

我想弄清楚如何检索记录,但由于我的知识有限,我似乎无法弄清楚如何检索具有相同 ID 的多行。如果您能提供帮助,我将不胜感激。我知道我可以使用查询来做到这一点,但我希望能够在需要时修改列表并将更改反映在列表中。

请参阅下面的 link 示例 sheet: https://docs.google.com/spreadsheets/d/15d-h5mDzOqSVPwZgBTudlxqWBTxRUa2sFNZYflE7vMo/edit?usp=sharing

function searchRecord() {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var shData = ss.getSheetByName ('list');
  var shDetail = ss.getSheetByName('detail'); 

  // Record 
  var searchValue = shData.getRange('B3:C3').getValue(); // Name to match 
  var values    = shData.getDataRange().getValues(); //getting the entire values from the used range and assigning it to values variable

  var valuesFound=false; //variable to store boolean value

  for (var i = 0; i < values.length; i++) {

    var rowValue = values[i]; //declaraing a variable and storing the value
 
    //checking the first value of the record is equal to search item
    if (rowValue[0] == searchValue) {

      // Navigate to Details Sheet
      try{
      ss.setActiveSheet(ss.getSheetByName('detail'));
       }  catch(err){
      }
      
      // Retrive Records
      
        shDetail.getRange('C2').setValue(rowValue[1]); // Name
        shDetail.getRange('C4').setValue(rowValue[0]); //ID
        shDetail.getRange('C6').setValue(rowValue[3]); // Price
        shDetail.getRange('B9:C15').setValue(rowValue[2]);

      return; //come out from the search function
      
      }
  }
    if(valuesFound==false){
  var ui = SpreadsheetApp.getUi();
  ui.alert('No record found!');
  return;
  }
}

你的情况,下面的修改怎么样?

修改点:

  • 在您的脚本中,当使用 shDetail.getRange('B9:C15').setValue(rowValue[2]); 时,仅使用“C”列的一个值。这样,所有行都是一个值。我认为这可能是您遇到问题的原因。而且,当我看到您的 Spreadsheet 时,我确认列“B”和“C”已合并。

根据你的情况,我想提出以下流程。

  1. 从“列表”的单元格“A6:D”中检索值sheet。
  2. 创建一个对象来搜索“列表”的“B3”的值sheet。
  3. 使用对象将“C2”、“C4”、“C6”和“B9:B15”的每个单元格的值放入“详细信息”sheet。

当这个流程反映到脚本中时,就变成了下面这样。

修改后的脚本:

function searchRecord() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var shData = ss.getSheetByName('list');
  var shDetail = ss.getSheetByName('detail');

  // I modified below script.
  var searchValue = shData.getRange('B3').getValue(); // Cells B3:C3 are merged.

  // 1. Retrieve values from the cells "A6:D" from "list" sheet.
  var values = shData.getRange("A6:D" + shData.getLastRow()).getValues();
  
  // 2. Create an object for searching value of "B3" of "list" sheet.
  var { obj } = values.reduce((o, [a, b, c, d]) => {
    if (a != o.temp && a != "") {
      o.temp = a;
      o.obj[a] = { "C4": a, "C2": b, "B9:B15": [[c]], "C6": d };
    } else {
      o.obj[o.temp]["B9:B15"].push([c]);
    }
    return o;
  }, { obj: {}, temp: "" });
  
  // 3. Put the values to each cells of "C2", "C4", "C6" and "B9:B15" to the "detail" sheet using the object.
  Object.entries(obj[searchValue]).forEach(([k, v]) =>
    shDetail.getRange(k).setValues(k == "B9:B15" ? v.length != 7 ? [...v, ...Array(7 - v.length).fill([null])] : v : [[v]])
  );
}
  • 在此修改后的脚本中,值是从“列表”sheet 的列“C”中作为数组检索的。通过这种方式,这些值可以放入“detail”sheet.
  • 的单元格“B9:B15”

参考文献:

检索多行并显示详细信息

function searchRecord() {
  const ss = SpreadsheetApp.getActive();
  const dat = ss.getSheetByName('list');
  const det = ss.getSheetByName('detail');
  const searchValue = dat.getRange('B3').getValue();
  const vs = dat.getRange("A6:D" + dat.getLastRow()).getValues();
  let f = { pA: [] };
  let prev = '';
  //collect detail info
  vs.forEach(r => {
    if (r[0] == searchValue || prev == searchValue) {
      if (!f.hasOwnProperty(r[0])) {
        f[r[0]] = { name: r[1], id: r[0], price: r[3], ingredients: [] };
        f[r[0]].ingredients.push(r[2]);
        f.pA.push(r[0]);
      } else {
        f[r[0]].ingredients.push(r[2]);
      }
      if(r[0])prev = r[0];
    }
  });
  //display detail
  if(f.pA && f.pA.length == 1) {
    det.getRange('C2').setValue(f[pA[0]].name);
    det.getRange('C4').setValue(f[pA[0]].id);
    det.getRange('C6').setValue(f[pA[0]].price);
    det.getRange(9,2,f[pA[0]].ingredients.length,1).setValues(f[pA[0]].ingredients)
  }
}