0

我使用费用报告教程来生成我的大部分代码。

我更改的主要内容是费用报告 ID 不是行号,而是我将其设置为随机数 (ll-nnnn)。我想使用报表 ID 作为费用报表表和费用审批表之间的标识符,所以我在报表表中添加了一个报表 ID 列。我希望脚本从批准表单中获取报告 ID,并在报告表 ID 列中搜索具有该 ID 的行,然后输入它是被批准还是被拒绝,以及经理在相应行中的任何评论M 和 N 列。在 VBA 中,我将使用的代码是带有匹配条件的 if 语句。我是 java 脚本的新手,所以我不知道该怎么做。***我已经使用 fooby 提供的内容编辑了我的代码。当我运行它时,我收到错误“范围的坐标或尺寸无效”。上线:“ poSheet.getRange(rowToUpdate, 2, 1, 13).setValues(updateInfo);” 任何建议将不胜感激!

    var PO_SPREADSHEET_ID= "0At0Io3p64FeddFVCV2lJdEpLY09MYWNkVS05NDhzWkE";
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];
    var poSs = SpreadsheetApp.openById(PO_SPREADSHEET_ID);
    var poSheet = poSs.getSheets()[0];

    function updatePOSheet() {
    // Get IDs from Approval Sheet
    var row = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
    // Get IDs from Report Sheet, flatten it into a 1D array
    var poId = poSheet.getRange(2,1,poSheet.getLastRow()-1,1).getValues().reduce(flatten_);
   // For each row in the Approval Sheet update the coresponding report row
   // reportIds is passed a "this" in the forEach function, see below
      row.forEach(updateReportRow_,poId);

      }

   // Checks to see if the status of a row is either "Approved" or "Denied
  // then updated the correct row in the reports sheet with manager's comments
     function updateReportRow_(row) {
     var id = row[2];
     var status = row[3];
     var comments = row[4];

  // Get row in reports sheet from reportIds (it was passed as 'this')
     var rowToUpdate = this.indexOf(id) + 1; 

  // Put info into spreadsheet friendly array
     var updateInfo = [[status,comments]];

     if (status == "Approved" || status == "Denied") {
     poSheet.getRange(rowToUpdate, 2, 1, 13).setValues(updateInfo);
       }
     }

  // Returns a piece of an array concatenated with the element on to it's right
  // will make [[0],[1],[2]...]] into [0,1,2,...]
  function flatten_(a,b) {
  return a.concat(b);
  }
4

1 回答 1

1

我一直在使用这种技术,在这里我试着解释一下,但效果并不好。因此,我尽我所能整理了一份演示表,说明如何实现自己的愿望。在我开始之前,这里是该方法的一般要点和未注释的代码。

  1. 使用onFormSubmit()触发器。
  2. 从目标工作表中获取 Ids 列。
  3. 将生成的二维数组展平为一维数组以便于搜索 (indexOf)
    • 我使用了 JS 迭代函数reduce()和一个名为flatten_(a,b)
    • 您可以在MDN找到更多详细信息。
  4. 组织来自事件的信息。
  5. 更新目标工作表中的相应行。

关于演示表的一些注意事项:

  • 它们是可编辑的;所以,请尝试一下,但要友善。
  • 报告表(目标表)有一些硬编码值。OP 正在用我没有构建的表格填写此内容。
  • 审批表不智能,你必须写一个现有的ID。最终,需要一个定制的 GUI 才能使这变得非常流畅。
  • 您可以在此处找到电子表格报告表| 审批表

这是未注释的代码:

function updateReportRow(approvalInfo) {
  var id = approvalInfo.namedValues.ID;
  var status = approvalInfo.namedValues.Status;
  var comments = approvalInfo.namedValues.Comments;
  var reportSheet = SpreadsheetApp.openById("SheetID").getSheetByName("Report Sheet");
  var reportIds = reportSheet.getRange(1,1,reportSheet.getLastRow(),1).getValues().reduce(flatten_);
  var rowToUpdate = reportIds.indexOf(id.toString())+1;
  if (rowToUpdate < 2) {
    // Something went wrong...email someone!
  } else {
    if (status == "Approved") {
      var updateInfo = [[status,comments,"STATE_APPROVED"]];
      reportSheet.getRange(rowToUpdate, 5, 1, updateInfo[0].length).setValues(updateInfo);
    } else if (status == "Denied") {
      var updateInfo = [[status,comments,"STATE_DENIED"]];
      reportSheet.getRange(rowToUpdate, 5, 1, updateInfo[0].length).setValues(updateInfo);
    }
  }
}

function flatten_(a,b) {
  return a.concat(b);
}
于 2012-12-27T17:17:35.870 回答