0
votes

I'm designing a script that takes an object (jsonData[data]) and inputs its values into a different sheet based on which product it is.

Currently the script inputs all the data into a new row each time the form reaches a new stage, however the form goes through 4 stages of approval and so I'm finding each submission being entered into 4 different rows. Each submission has an "Id" value within the object which remains the same (but each submission could also be on any row in the sheet as it's used a lot).

I'm checking whether the ID exists in the sheet and using iteration to find the row number:

function updatePlatformBulkInfo(jsonData) {  
  var sheetUrl = "https://docs.google.com/spreadsheets/d/13U9r9Lu2Fq1WTT8pQ128heCm6_gMmH1R4O6u8e7kvBo/edit#gid=0"; 
  var sheetName = "PlatformBulkSetup";  
  var doc = SpreadsheetApp.openByUrl(sheetUrl);  
  var sheet = doc.getSheetByName(sheetName);
  var rowList = [];
  var formId = jsonData["Id"];
  var allSheetData = sheet.getDataRange().getValues();
  setLog("AllSheetData = " + allSheetData[1][11]) //Logs to ensure data is collected correctly
  var rowEdited = false;
  var rowNumber = 0;

  //Check whether ID exists in the sheet  
  for (var i = 0; i < allSheetData.length; i++) {
    if(allSheetData[i][11] == formId) {
      rowEdited = true;
    } else {
        rowNumber += 1;        
    } 
  }

My issue is with the next part:

//Append row if ID isn't duplicate or update row if duplicate found
if (rowEdited == false) {
  for (var data in jsonData) {
    rowList.push(jsonData[data])
  } 
  setLog("***Row List = " + rowList + " ***");
  setLog("***Current Row Number = " + rowNumber + " ***");
  sheet.appendRow(rowList); 
} else if(rowEdited == true){
  var newRowValue = jsonData[data];
  sheet.getRange(rowNumber, 1).setValues(newRowValue);
}

Everything works fine if the duplicate isn't found (the objects values are appended to the sheet). But if a duplicate is found I'm getting the error:

Cannot find method setValues(string)

This looks to me like i'm passing a string instead of an object, but as far as I'm aware I've already converted the JSON string into an object:

var jsonString = e.postData.getDataAsString();
var jsonData = JSON.parse(jsonString);

How can I modify my script to write the updated data to the matched row?

2
what do you see when you do console.log(sheet.getRange(rowNumber, 1)) ?Nope
setValues takes an Object[][] that will tile in the bound range. i.e an array of arrays.tehhowch
console.log(sheet.getRange(rowNumber, 1)) just returns 'Range'Adam Roberts
@tehhowch I only need the data from the object rather than an Object[][] (as I'm only moving the values that have been inputted into the sheet - is there any other way to do it?Adam Roberts
@AdamRoberts given that you want to write to a single cell a single value, you want to use Range#setValue() and not Range#setValues()tehhowch

2 Answers

1
votes

It's unclear based on your code whether or not you will actually write to the correct cell in the case of a duplicate. As presented, it looks as though you loop over the sheet data, incrementing a row number if the duplicate is not found. Then, after completing the loop, you write to the sheet, in the row described by rowNumber, even though your code as written changes rowNumber after finding a duplicate.

To address this, your loop needs to exit upon finding a duplicate:

var duplicateRow = null, checkedCol = /* your column to check */;
for(var r = 0, rows = allSheetData.length; r < rows; ++r) {
  if(allSheetData[r][checkedCol] === formId) {
    // Convert from 0-base Javascript index to 1-base Range index.
    duplicateRow = ++r;
    // Stop iterating through allSheetData, since we found the row.
    break;
  }
}

In both cases (append vs modify), you seem to want the same output. Rather than write the code to build the output twice, do it outside the loop. Note that the order of enumeration specified by the for ... in ... pattern is not dependable, so if you need the elements to appear in a certain order in the output, you should explicitly place them in their desired order.
If a duplicate ID situation is supposed to write different data in different cells, then the following two snippets will need to be adapted to suit. The general idea and instructions still apply.

var dataToWrite = [];
/* add items to `dataToWrite`, making an Object[] */

Then, to determine whether to append or modify, test if duplicateRow is null:

if(dataToWrite.length) {
  if(duplicateRow === null) {
    sheet.appendRow(dataToWrite);
  } else {
    // Overwriting a row. Select as many columns as we have data to write.
    var toEdit = sheet.getRange(duplicateRow, 1, 1, dataToWrite.length);
    // Because setValues requires an Object[][], wrap `dataToWrite` in an array.
    // This creates a 1 row x N column array. If the range to overwrite was not a
    // single row, a different approach would be needed.
    toEdit.setValues( [dataToWrite] );
  }
}
0
votes

Below is the most basic solution. At the end of this post, I'll expand on how this can be improved. I don't know how your data is organized, how exactly you generate new unique ids for your records, etc., but let's assume it looks something like this.

enter image description here

Suppose we need to update the existing record with new data. I assume your JSON contains key-value pairs for each field:

    var chris = {
    id:2, 
    name: "Chris", 
    age: 29, 
    city: "Amsterdam" 
    };

Updating a record breaks down into several steps:

1) Creating a row array from your object. Note that the setValues() method accepts a 2D array as an argument, while the appendRow() method of the Sheet class accepts a single-dimension array.

2) Finding the matching id in your table if it exists. The 'for' loop is not very well-suited for this idea unless you put 'break' after the matching id value is found. Otherwise, it will loop over the entire array of values, which is redundant. Similarly, there's no need to retrieve the entire data range as the only thing you need is the "id" column. IMPORTANT: to get the row number, you must increment the array index of the matching value by 1 as array indices start from 0. Also, if your spreadsheet contains 1 or more header rows (mine does), you must also factor in the offset and increment the value by the number of headers.

3) Based on the matching row number, build the range object for that row and update values. If no matching row is found, call appendRow() method of the Sheet class.

function updateRecord(query) {

  rowData = [];

   var keys = Object.keys(query);
   keys.forEach(function(key){

       rowData.push(query[key]); 

    })

  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheets()[0];

  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];

  var idColumn = 1;

  var ids = sheet.getRange(2, idColumn, sheet.getLastRow() - 1, 1).getValues();

  var i = 0;
  var matchedRow;

  do {

    if (ids[i] == query.id) { matchedRow = i + 2; }

    i++;

  } while (!matchedRow && i < ids.length);

  if (matchedRow) { 

   var row = sheet.getRange(matchedRow, idColumn, 1, rowData.length);
   row.setValues([rowData]);

  } else {

    sheet.appendRow(rowData);

  }

}

NOTE: if your query contains only some fields that need to be updated (say, the 'id' and the 'name' field), the corresponding columns for these fields will be

headers.indexOf(query[key]) + 1;

Possible improvements

If the goal is to use the spreadsheet as a database and define all CRUD (Create, Read, Write, Delete) operations. While the exact steps are beyond the scope of the answer, here's the gist of it.

1) Deploy and publish the spreadsheet-bound script as a web app, with the access set to "anyone, even anonymous".

    function doGet(e) {

    handleResponse(e);

    }

    function doPost(e) {

    handleRespone(e);

    }

    function handleResponse(e) {

    if (e.contentLength == -1) {
       //handle GET request
     }  else {
          //handle POST request
        }

     }

2) Define the structure of your queries. For example, getting the list of values and finding a value by id can be done via GET requests and passing parameters in the url. Queries that add, remove, or modify data can be sent as payload via POST request. GAS doesn't support other methods besides GET and POST, but you can simulate this by including relevant methods in the body of your query and then selecting corresponding actions inside handleResponse() function.

3) Make requests to the spreadsheet URL via UrlFetchApp. More details on web apps https://developers.google.com/apps-script/guides/web