简体   繁体   English

使用数组循环将值与另一个电子表格进行比较并写入新值

[英]Compare value to another spreadsheet using array loop and write new values

Hello all I'm having trouble implementing array loops in my project... Here is what I want to do. 您好,我在我的项目中无法实现数组循环...这是我想要做的。

I have a spreadsheet called "Red Book" this sheet gets updated regularly once the staff have updated it I have a column where they can select to submit the data they've just entered on that specific row (editing this column calls an onEdit function). 我有一个名为“ Red Book”的电子表格,一旦工作人员对其进行了更新,该表就会定期更新。我有一列,他们可以选择提交刚刚在该特定行上输入的数据(编辑此列称为onEdit函数) 。

The data will then be written to another spreadsheet (different file) called "Raw Data" 然后,数据将被写入另一个名为“原始数据”的电子表格(不同的文件)

For each submit I have a unique identifier. 对于每个提交,我都有一个唯一的标识符。 I need the onEdit code to do the following... 我需要onEdit代码来执行以下操作...

  1. Iterate through the column A to find the unique identifier 遍历列A以查找唯一标识符
  2. Once found update the data in columns 1 through 5 找到后,更新第1至5列中的数据

Below is the script I have so far: 以下是到目前为止的脚本:

function TransferToAppData(e) {
  var destFile = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  var destSheet = destFile.getSheetByName("Raw App Data");
  var ss = e.source;
  var s = ss.getActiveSheet();
  var uniConstRng = s.getRange("A1");
  var uniqueConstVal = uniConstRng.getValue();
  var NextOpenRow = destSheet.getLastRow() + 1;
  var ActiveRow = e.range.getRow();
  Logger.log(ActiveRow);
  var uniqueVal = s.getRange(ActiveRow,1).getValue();
  var add = s.getRange(ActiveRow,2).getValue();
  var name = s.getRange(ActiveRow,3).getValue();
  var dt = s.getRange(ActiveRow,5).getValue()
  if (uniqueVal == "") {
    s.getRange(ActiveRow,1).setValue(uniqueVal + 1);
    uniConstRng.setValue(uniqueVal + 1);
    var transferVals = s.getRange(ActiveRow,1,1,5).getValues();
      Logger.log(transferVals);
    destSheet.getRange(NextOpenRow,1,1,5).setValues(transferVals);
    destSheet.getRange(NextOpenRow, 6).setValue("Applicant");
  }
  else {
    var destLastRow = destSheet.getLastRow();
    var destDataRng = destSheet.getRange(2,1,destLastRow,5)
    var destValues = destDataRng.getValues();
    var sourceValues = s.getRange(ActiveRow,1,1,5).getValues();
    for( var i = 0; i < destValues.length; ++i){
      if (destValues([i][0])==uniqueVal) {
        for(n=0;n<destValues[0].length;++n){
          ///I"m stuck!!!

        }
      }
    }
  }
}

As you can see I have the first array loop going, but I'm having trouble figuring out how to do a second loop that iterates only on the row where the unique value is found and write the source data to ONLY to row where the unique value was found not the whole sheet . 如您所见,我正在执行第一个数组循环,但是我很难弄清楚如何执行仅在找到唯一值的行上进行迭代的第二个循环,并将源数据写入唯一的行发现值不是整个表

I figured it out... 我想到了...

Below is the code and here is how it works... 下面是代码,这是它的工作方式...

When values in certain columns are edited this code is fired. 编辑某些列中的值时,将触发此代码。

1--It finds the unique identifier located in the row which was edited. 1-查找位于已编辑行中的唯一标识符。

2--Compares that identifier with a column of unique identifiers in another spreadsheet. 2-比较该标识符和另一个电子表格中的一列唯一标识符。

3--When a match is found it writes the change to the new spreadsheet and exits the loop 3--找到匹配项后,它将更改写入新的电子表格并退出循环

function TransferToAppData(e) {
  var destFile = SpreadsheetApp.openById('1V3R2RnpA8yXmz_JDZSkBsK9tGR2LjHZp52p5I1CuQvw');
  var destSheet = destFile.getSheetByName("Raw App Data");
  var ss = e.source;
  var s = ss.getActiveSheet();
  var uniqueConstRng = s.getRange("A1");
  var uniqueConstVal = uniqueConstRng.getValue();
  var NextOpenRow = destSheet.getLastRow() + 1;
  var ActiveRow = e.range.getRow();
  var uniqueVal = s.getRange(ActiveRow,1).getValue();
  if (s.getRange(ActiveRow,2).getValue() == "" || s.getRange(ActiveRow,3).getValue()=="" || s.getRange(ActiveRow,4).getValue()=="" || s.getRange(ActiveRow,5).getValue()=="") {
    s.getRange(ActiveRow,13).clearContent();
    Browser.msgBox("Address, Name, Date Entered & Rent are required fields!");
  } else{
    if (uniqueVal == "") {
      s.getRange(ActiveRow,1).setValue(uniqueConstVal + 1);
      uniqueConstRng.setValue(uniqueConstVal + 1);
      var transferVals = s.getSheetValues(ActiveRow,1,1,5);
      destSheet.getRange(NextOpenRow,1,1,5).setValues(transferVals);
      destSheet.getRange(NextOpenRow, 6).setValue("Applicant");
    }
    else {
      var destLastRow = destSheet.getLastRow();
      var destValues = destSheet.getSheetValues(2,1,destLastRow,5);
      var sourceValues = s.getSheetValues(ActiveRow,1,1,5);
      for(var i = 0; i < destValues.length; ++i){
        if (destValues[i][0]===uniqueVal) {
          destSheet.getRange(i+2,1,1,5).setValues(sourceValues);
          break;
          }
        }
      }
     s.sort(1,false);
     destSheet.sort(1,false);
   }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM