How do I update specific rows in Google Spreadsheets using Reactjs?

后端 未结 1 561
误落风尘
误落风尘 2021-01-13 12:19

What needs to happen is that from my React web app there\'s this form and data inputted should be sent to Google Spreadsheet.

  1. Is there away to avoid using Time
相关标签:
1条回答
  • 2021-01-13 12:56

    I'm dividing this answer in two parts:

    1. Get rid of the Timestamp

    As you see in your Google Appscript code:

    var row = [new Date()]; // first element in the row should always be a timestamp
    

    You just have to remove the new Date() function at leave it as an empty array declaration:

    var row = []; //There is no first element yet.
    

    Right after this there is a for loop whose index is starting from 1 to avoid the Timestamp, but since we have removed it there is no need to start at 1, so we have to change it.

     for (var i = 0; i < oldHeader.length; i++) { // Now we start at 0 instead of 1
          var field = oldHeader[i];
          var output = getFieldFromData(field, e.parameters);
          row.push(output);
          ....
    

    2. Updating a cell depending on the condition

    Since you already know the Name's condition (Joe) to update the Number, we just have to compare the values of each cell in A with the String 'Joe':

    function updateCell() {
    
      var doc = SpreadsheetApp.getActiveSpreadsheet(); //this line is not necessary if you just paste this code in yours
    
      var names = doc.getRange("A2:A").getValues(); //We store all the names from A2 to the last one
      var newValue = '456';
    
      for (var n = 2; n < names.length + 2; n++){ //We start at 2 since A1 and B1 are the headers of the table
    
        if (names[n - 2] == 'Joe'){ //But the array's first position is 0, so we deduct 2 form n.
    
          doc.getRange("B" + (n)).setValue(newValue);
        }
      }
    }
    

    Result:

    0 讨论(0)
提交回复
热议问题