如果是,则连续匹配2个范围之间的值,并将值粘贴到不同的列中

时间:2019-04-17 10:20:17

标签: google-apps-script google-sheets

我有一个带有2个标签的BOM表和POProcess的google工作表。我想通过在BOM表的D列值中查找POProcess表的B列值(连续一个接一个)来复制PO Process表中的PO号,如果匹配,则将BOM号粘贴在BOM表的M列中(对于所有匹配的值)。这是工作表的链接。

https://docs.google.com/spreadsheets/d/1MqYn2AjPncx-RvvyTS8Nj0ujpm9Lcd_ORJF9Wqbw6y0/edit?usp=sharing

我尝试了以下代码,但由于它无法检查“采购订单流程”工作表中的唯一一个值,因此无法正常工作。请帮助如何迭代PO流程表中的值

function UpdateStatus(){

var sss = SpreadsheetApp.getActiveSpreadsheet();
var ss = sss.getSheetByName('POProcess'); //replace with source Sheet tab name
var range = ss.getRange('C4'); //assign the range you want to copy
var data = range.getValues();

var range2 = ss.getRange('B8'); //value to look for to be replaced
var data2 = range2.getValues();

var sheet = sss.getSheetByName('BOMSheet');
var range3 = sheet.getRange('A:L');
var values = range3.getValues();
for (var i = 0; i < values.length; i++) {
  if (values[i][3] == data2) {
   values[i][12] = data;

  }
}
range3.setValues(values);

}

1 个答案:

答案 0 :(得分:0)

此功能只能用于采购订单编号。

function UpdateStatus() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var pSheet = ss.getSheetByName('POProcess'); //replace with source Sheet tab name
  var pNumber = pSheet.getRange('C4').getValue(); //assign the range you want to copy

  var pRange = pSheet.getRange(8, 2, pSheet.getLastRow()-7, 1); //value to look for to be replaced
  var pData = pRange.getValues().map(function(el) {
    return el[0];
  });

  var sheet = ss.getSheetByName('BOMSheet');
  var range3 = sheet.getDataRange();
  var values = range3.getValues();

  values.forEach(function(row) {
    if (row[3] == '') return;
    if (pData.indexOf(row[3]) > -1) {
      row[12] = pNumber;
    }
  });

  range3.setValues(values);
}

对于映射“已接收的数量”列,也请尝试此操作。

function UpdateStatus() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var pSheet = ss.getSheetByName('POProcess'); //replace with source Sheet tab name
  var pNumber = pSheet.getRange('C4').getValue(); //assign the range you want to copy

  var pRange = pSheet.getRange(8, 2, pSheet.getLastRow() - 7, pSheet.getLastColumn()-1); //values to map
  var pData = {};
  pRange.getValues().forEach(function(row) {
    pData[row[0]] = row[6];
  });

  var sheet = ss.getSheetByName('BOMSheet');
  var range3 = sheet.getDataRange();
  var values = range3.getValues();

  values.forEach(function(row) {
    if (row[3] == '') return;
    if (typeof pData[row[3]] != 'undefined') {
      row[12] = pNumber;
      row[13] = pData[row[3]];
    }
  });

  range3.setValues(values);
}