如何更新工作表中新提交的表单响应上的字段?

时间:2019-10-21 18:20:33

标签: google-apps-script google-sheets-api

我有一个正在使用的Google表格和表格。当用户提交表单时,我在运行的工作表的OnFormSubmit中有代码。使用他们提交的ID,我可以在该工作表的第二个标签中查找数据并将其通过电子邮件发送给某人。

我想将我查找的那些值添加到响应表中。这可能吗?

function onFormSubmit(e) 
{

  var myRange = e.range;
  sendEmail(myRange);

}

function sendEmail(submitRange) {

  // FETCH SPREADSHEET VALUES//
  var values = submitRange.getValues();
  var row = values[0];

  //WRITE FOUND VALUES TO EMAIL
  var svTimeStamp = row[0];  //A
  var svProjectManager = row[4]; //Update manually  //E
  var svClient = row[5]; //Update manually  //F

  //PREP LOOKUP VALUE SHEET
  var mySpreadSheetFile = SpreadsheetApp.openById('--SheetId---');
  var myLookupsTab = mySpreadSheetFile.getSheetByName("KMB Lookups");


  //In Column 6 find value for Project Number
  var projColumn = 6;
  var projectNumbers = myLookupsTab.getRange(2, projColumn, myLookupsTab.getLastRow()).getValues(); //1st is header row
  var rowIndex = projectNumbers.findIndex(svProjectID); //Row Index - 2

  //Get value for column 7
  var clntColumn = 7;
  var clntRange = myLookupsTab.getRange(rowIndex+2, clntColumn);
  var clntValue = clntRange.getValues();

  //I don't think I can update 'Values' from 'submitRange, so I open the sheet
  var myData = mySpreadSheetFile.getSheetByName("Form Responses 1");
  var newestData = myData.getRange(2, 1, myData.getLastRow()).getValues(); //1st is header row
  //FIND THE RECORD VIA MATCHING THE TIMESTAMP
  var rowNewDataIndex = newestData.findIndex(svTimeStamp); //Row Index - 2
  //UPDATE THE SHEET @ Rowindex, column 5
  myData.getRange(rowNewDataIndex+2, 5).setValue(clntValue);

我希望该单元格将使用查找值进行更新。每次rowNewDataIndex的值为-14。我猜该行尚未写过...

编辑:我认为问题是由于格式不匹配造成的:

RANGE: Mon Oct 21 2019 15:24:00 GMT-0400 (EDT)
SHEET: Mon Oct 21 16:07:52 GMT-04:00 2019

这似乎可行:

var formattedDate = Utilities.formatDate(svTimeStamp, Session.getScriptTimeZone(), 'E MMM dd k:mm:ss zZ yyyy');
formattedDate = formattedDate.toString().replace("EDT","GMT");
formattedDate = formattedDate.toString().replace("-0500","-05:00");
formattedDate = formattedDate.toString().replace("-0400","-04:00");

然后我可以在现有电子表格中搜索该值。

1 个答案:

答案 0 :(得分:1)

这是我清理过的代码。通过将范围发送到数组,我能够匹配日期。走吧。

function onFormSubmit(e) 
{
  var myRange = e.range;
  sendEmail(myRange);
}

function sendEmail(submitRange) {

  // FETCH SPREADSHEET //
  var values = submitRange.getValues();
  var row = values[0];

  //WRITE FOUND VALUES TO EMAIL
  var svProjectManager = row[4]; //E
  var svClient = row[5]; //F

  // EXTRACT VALUES //
  var svTimeStamp = row[0];  //A
  var svInitials = row[1];  //B
  var svProjectID = row[2];  //C


  //LOOKUP KMB PROJECT NUMBER IN SHEET: "New - KMB Design Visit Closeout (Responses)" TAB: "KMB Lookups"
  var mySpreadSheetFile = SpreadsheetApp.openById('---Spreadsheet ID---');
  var myLookupsTab = mySpreadSheetFile.getSheetByName("KMB Lookups");

  //TODO - Need to code column numbers by looking for column-- column order may change.
  //In Column 5 'KMB Project ID' find record where value = svProjectID
  var projColumn = 5;
  var projectNumbers = myLookupsTab.getRange(2, projColumn, myLookupsTab.getLastRow()).getValues(); //1st is header row
  var rowIndex = projectNumbers.findIndex(svProjectID); //Row Index - 2

  //Get value for column named 'Client Assigning this Work' col 7
  var clntColumn = 7;
  var clntRange = myLookupsTab.getRange(rowIndex+2, clntColumn);
  var clntValue = clntRange.getValues();

  //Get value for column named 'Site Address'  col 28
  var addrColumn = 28;
  var addrRange = myLookupsTab.getRange(rowIndex+2, addrColumn);
  var addrValue = addrRange.getValues();

  //Get value for column named 'KMB Project Manager' col 11
  var pmgrColumn = 11; 
  var pmgrRange = myLookupsTab.getRange(rowIndex+2, pmgrColumn);
  var pmgrValue = pmgrRange.getValues();


  //GET SITETRACKER IDS AND WRITE TO SPREADSHEET
  console.log('Writing to spreadsheet');
  var IdColumn = 1; //Hearing Results
  var IDRange = myLookupsTab.getRange(rowIndex+2, IdColumn);
  var nameIdValue = IDRange.getValues();
  console.log(nameIdValue);

  IdColumn = 2; 
  var IDRange = myLookupsTab.getRange(rowIndex+2, IdColumn);
  var IdSiteNameValue = IDRange.getValues();
  console.log(IdSiteNameValue);

  IdColumn = 3; 
  var IDRange = myLookupsTab.getRange(rowIndex+2, IdColumn);
  var IdProjectIDValue = IDRange.getValues();
  console.log(IdProjectIDValue);

  console.log('Form Timestamp: '+svTimeStamp);


      //Update myData where field TimeStamp = svTimeStamp.  Set Name Id, Id Site Name, & Id Project Id  [FUZZLE]
  var myData = mySpreadSheetFile.getSheetByName("Form Responses 1");
  var newestData = myData.getRange(2, 1, myData.getLastRow()).getValues().toString(); //This is the TimeStamp column values without header
  newestData = newestData.split(",");
  console.log(newestData);
  var rowNewDataIndex = newestData.findIndex(svTimeStamp); //Row Index - 2
  console.log('Row to update = '+rowNewDataIndex);

  myData.getRange(rowNewDataIndex+2, 33).setValue(nameIdValue);
  myData.getRange(rowNewDataIndex+2, 34).setValue(IdSiteNameValue);
  myData.getRange(rowNewDataIndex+2, 35).setValue(IdProjectIDValue);


  var svSiteAddress = addrValue;  //D
  var svProjectManager = pmgrValue; //E
  var svClient = clntValue;  //F

  var svNOCNeeded = row[6];  //G
  var svNOCInCode = row[7];  //H
  var svNOCOutCode = row[8];  //I
  var svPurpose = row[9];  //J
  var svDVStatus = row[10];  //K
  var svWhyIncompleteDV = row[11];  //L
  var svFDNYReq = row[12];  //M
  var svFDNYStatus = row[13];  //N
  var svFDNYWhyNot = row[14];  //O
  var svLPCNeeded = row[15];  //P
  var svLPCStatusPhotos = row[16];  //Q
  var svLPCStatusRevisit = row[17];  //R
  var svDesignRevisitNeeded = row[18];  //S
  var svWhyRevisitPROBE = row[19];  //T
  var svWhyRevisitSTRUCT = row[20];  //U
  var svWhyRevisitELEC = row[21];  //V
  var svWhyRevisitPLUM = row[22];  //W
  var svWhyRevisitWIRE = row[23];  //X
  var svWhyRevisitOTHER = row[24];  //Y
  var svStructuralReview = row[25];  //Z
  var svStructuralReviewWhy = row[26];  //AA
  var svVisitComplete = row[27];  //AB
  var svWhyIncompleteSV = row[28];  //AC
  var svProvideAccessInfo = row[29];  //AD
  var svComments = row[30];  //AE
  var svHearingResults = row[31];  //AF

  // PREPARE EMAIL //
  var emailBody = "";  //Format using variables set

  //BUILD EMAIL BODY
  emailBody="<h3><u>Site Information</u></h3>"
  emailBody+="<p><strong>KMB Project Number:&nbsp;</strong>"+svProjectID+"</p>";
  emailBody+="<p><strong>Site Address:&nbsp;</strong>"+svSiteAddress+"</p>";
  emailBody+="<p><strong>Project Manager:&nbsp;</strong>"+svProjectManager+"</p>";
  emailBody+="<p><strong>Client Assigning Work:&nbsp;</strong>"+svClient+"</p>";
  emailBody+="<p><strong>Design Visit Type:&nbsp;</strong>"+svPurpose+"</p>";
  emailBody+="<p>&nbsp;</p>";

  //NOC SECTION
  if (svNOCNeeded == "Yes")
  {
    emailBody+="<h3><u>NOC Code Entry</u></h3>";
    emailBody+="<p>NOC In Code:&nbsp;"+svNOCInCode+"</p>";
    emailBody+="<p>NOC Out Code:&nbsp;"+svNOCOutCode+"</p>";
    emailBody+="<p>&nbsp;</p>";
  }

  //DESIGN VISIT STATUS SECTION
  if (svPurpose == "Design Visit")
  {
    emailBody+="<h3><u>Design Visit Status</u></h3>";
    emailBody+="<p>"+svDVStatus+"</p>";

    if(svDVStatus == "Not Completed")
    {
      emailBody+="<p>Reason: "+svWhyIncompleteDV+"</p>";
    }
    emailBody+="<p>&nbsp;</p>";
  }

  //FDNY SECTION
  if (svFDNYReq != "")
  {
    emailBody+="<h3><u>FDNY</u></h3>";
    emailBody+="<p>Required:&nbsp;"+svFDNYReq+"</p>";

    if(svFDNYReq == "Required")
    {
      emailBody+="<p>Info Status Gathering:&nbsp;"+svFDNYStatus+"</p>";
    }
    else
    {
      emailBody+="<p>Reason:&nbsp;"+svFDNYWhyNot+"</p>";
    }
    emailBody+="<p>&nbsp;</p>";
  }

  //LPC SECTION
  if (svLPCNeeded != "")
  {
    emailBody+="<h3><u>LPC&nbsp;</u></h3>";
    emailBody+="<p>Needed:&nbsp;"+svLPCNeeded+"</p>";
    if (svLPCNeeded == "Yes")
    {
      emailBody+="<h4>Status</h4>";
      emailBody+="<p>LPC Photos Taken:&nbsp;"+svLPCStatusPhotos+"</p>";
      emailBody+="<p>Revisit Needed:&nbsp;"+svLPCStatusRevisit+"</p>";
    }
    emailBody+="<p>&nbsp;</p>";
  }

  //DESIGN REVISIT NEEDED SECTION
  if (svDesignRevisitNeeded == "Yes")
  {
    emailBody+="<h3><u>Design Revisit Needed</u></h3>";
    emailBody+="<p>Probe:&nbsp;"+svWhyRevisitPROBE+"</p>";
    emailBody+="<p>Structural Revisit:&nbsp;"+svWhyRevisitSTRUCT+"</p>";
    emailBody+="<p>Electrical Revisit:&nbsp;"+svWhyRevisitELEC+"</p>";
    emailBody+="<p>Plumbing Revisit:&nbsp;"+svWhyRevisitPLUM+"</p>";
    emailBody+="<p>Wireless Revisit:&nbsp;"+svWhyRevisitWIRE+"</p>";
    emailBody+="<p>Other:&nbsp;"+svWhyRevisitOTHER+"</p>";
    emailBody+="<p>&nbsp;</p>";
  }

  //STRUCTURAL REVIEW
  if (svStructuralReview == "Yes")
  {
    emailBody+="<h3><u>Structural Review</u></h3>";
    emailBody+="<p>Reason:&nbsp;"+svStructuralReviewWhy+"</p>";
    emailBody+="<p>&nbsp;</p>";
  }

  //VISIT COMPLETE
  if (svVisitComplete != "")
  {
    emailBody+="<h3><u>Visit Complete</u></h3>";
    emailBody+="<p>"+svVisitComplete+"";
    if (svVisitComplete == "No")
    {
      emailBody+="&nbsp;("+svWhyIncompleteSV+")";
    }
    emailBody+="</p><p>&nbsp;</p>";
  }
  //ACCESS INFO / COMMENTS SECTION
  emailBody+="<h3><u>Access Info and Comments</u></h3>";
  emailBody+="<p>Information Used to access this site:&nbsp;"+svProvideAccessInfo+"</p>";
  emailBody+="<p>General Design Visit Comments:&nbsp;"+svComments+"</p>";
  emailBody+="<p>&nbsp;</p>";

  //ZONING HEARING SECTION
  if (svPurpose == "Hearing")
  {
    emailBody+="<h3><u>Zoning Hearing Results</u></h3>";
    emailBody+="<p>"+svHearingResults+"</p>";
  }
  //Logger.Log(emailBody);
  var emailRecipients =   "user@comain.com";
  var emailSubject = "Field Visit Completed for "+svProjectManager+" "+svProjectID;

  // SEND EMAIL //
  MailApp.sendEmail(
    {
      name: svInitials,
      to: emailRecipients,
      subject: emailSubject,
      htmlBody: emailBody
    });
}

Array.prototype.findIndex = function(search)
{
  if(search == "") return false;
  for (var i=0; i<this.length; i++)
    if (this[i] == search) return i;

  return -1;
}