1

I am using Google sheets to store data that is updated from a web app.

I retrieve all the data by using getValues() on getDataRange() of the sheet, like this:

    // get reference to the data sheet
    var ss = _getPOMasterSpreadsheet();
    var shtData = ss.getSheetByName( "TestSheet11" );

    // obtain the grid
    var dataRange = shtData.getDataRange();
    var dataGrid = dataRange.getValues();

    var cols = dataGrid[0].length;
    var rows = dataGrid.length;

Sometimes I extend the the data, by adding new rows, but not every column in the row contains data, for example:

    // create a new row and add it to the grid
    var newRow = new Array( cols );
    dataGrid.push( newRow );

    // write a value to the last cell of the new row
    newRow[ newRow.length-1 ] = "NEW LAST CELL" ;

Then the newly extended grid is written back to the sheet using setValues()

    // get the range to be updated and set values from the grid
    var newDataRange = shtData.getRange(1, 1, rows + 1, cols);
    newDataRange.setValues( dataGrid );

This means that there are parts of the grid that have no value. When the grid is written to the sheet, the cells corresponding to undefined values are filled with the text "NOT_FOUND" instead of being blank:

Undefined grid locations are filled with NOT_FOUND

Is there some way to get these undefined values to be empty cells when writing back to the sheet? I was trying to avoid having a loop to fill all the undefined grid locations with zero length strings.

JohnRC
  • 1,251
  • 1
  • 11
  • 12
  • how are the cell filled with "NOT_FOUND" ?? Normally cells should be empty. – JSmith Oct 28 '18 at 17:42
  • When you use range.setValues( grid ), where grid is a 2d array of arrays, then wherever there are undefined grid elements, the setValues function fills the sheet cell with the string "NOT_FOUND" automatically. – JohnRC Oct 28 '18 at 19:06
  • well never had this problem, any documetation for that beahaviour? – JSmith Oct 28 '18 at 19:08
  • @JSmith There is no mention in the Google Apps Script documentation of what happens if a grid element is not defined. It seems to be an undocumented feature. – JohnRC Oct 28 '18 at 19:14
  • well just added an answer it's the only way I see. Sorry. – JSmith Oct 28 '18 at 19:19
  • Thanks for your help! I will do as you suggest. – JohnRC Oct 28 '18 at 19:19
  • please tell me if it worked. Best! – JSmith Oct 28 '18 at 19:20
  • Try Sheets API. Although I'm unsure how it'll react, I'm sure it'll react differently. https://stackoverflow.com/a/52872474 – TheMaster Oct 28 '18 at 19:37

1 Answers1

1

as @I'-'I told me fill is not available in app script.

You still can probably use it at client side so here what you shoul write:

newRow.fill(""); // or 0

That being the case If you code on server side I don't see any other solutions than using a forEachfunction as so:

   var newRow = new Array( cols );
   newRow.forEach(function(element, index)
   {
     newRow[index] = ""; // or 0
   });
   newRow[ newRow.length - 1] = "NEW LAST CELL" ;
   dataGrid.push( newRow );

You can go have a look at this post

REFERENCES

array.forEach()

JSmith
  • 4,519
  • 4
  • 29
  • 45
  • 1
    Thanks @JSmith I was just getting to the point of recoding to use fill when I saw your comment about fill not being available. AFAIK all my scripts are running on the server not the client - I did not know that there was any client-side scripting available in Sheets. Anyway, I will have to use the long-winded method. Many thanks for your help - much appreciated. – JohnRC Oct 30 '18 at 17:13
  • @JohnRC Your welcome! if you are using a html in your webapp you could easily parse through your datas client side then send them back to server using `google.script.run.myFunction(datas)` – JSmith Oct 30 '18 at 18:05
  • @JohnRC I've added an edit to my post. Please tell me if it worked for you. Best! – JSmith Nov 02 '18 at 02:08
  • Re your edit, the problem I have is with creating new rows which have undefined cells and setting values back in the sheet using `setValues()`. That's when the NOT_FOUND text appears. – JohnRC Nov 02 '18 at 14:38
  • @JohnRC oh your right sorry bout that I was off the basic problem. I'll correct my answer – JSmith Nov 02 '18 at 21:31