Why when update and create new row, existing data move to adjacent column

Hi, I am using CRUDQII example to update, and create new row, however when update - existing data in google sheet, move to the next adjacent column, similarly the create function also give same result. How do I fix this problem? Thank you in advance



You do not show your google sheet or your script.

Do you have an automatic indexing formula in column A on your sheet, and are you also creating an index in your data before applying it ?

function doGet(e) {

 

  var ss = SpreadsheetApp.openById(e.parameter.ID);

  var sh = ss.getSheetByName(e.parameter.SH);

  var fn = e.parameter.FN;

  var rg = sh.getDataRange().getValues();

 

  // Create/Add new record, using comma separated values

  if ( fn == 'CREATE' ) {

    var data = e.parameter.DATA.split(',');

    sh.appendRow(data);

    return ContentService.createTextOutput("New record created");

  }

 

  // Reads/Returns all data as a stringified JSON List

  else if ( fn == 'READ' ) {

    return ContentService.createTextOutput(JSON.stringify(rg));    

  }

 

  // Edit/Update existing record, requires index/row and current col1 to match

  else if ( fn == 'UPDATE' ) {

    var index = e.parameter.INDEX;  //index in list

    var col1 = e.parameter.COL1;  // current/existing value of col1 - it could be replaced...

    var data = e.parameter.DATA.split(','); //new data

    var range = sh.getRange((parseInt(index)+1),1,1,data.length);

    for (var i = 0; i < rg.length; i++ ) {

      if ( index != undefined && i == index && col1 == rg[i][0] ) {

        range.setValues([data]);

      }

    }

    return ContentService.createTextOutput("Record updated");    

    }

 

  // deletes a single record (and its row) from sheet. Requires row index and col1 to match

  else if ( fn == 'DELETE' ) {

    var index = e.parameter.INDEX;  //index in list

    var col1 = e.parameter.COL1;  // current/existing value of col1 - it could be replaced...

    for (var i = 0; i < rg.length; i++ ) {

      if ( index != undefined && i == index && col1 == rg[i][0] ) {

        sh.deleteRow(parseInt(index)+1);

      }

    }

    return ContentService.createTextOutput("Existing record deleted");    

   }

   

  // outputs results from SQL query of all data  

  else if ( fn == 'QUERY' ) {

    var rgq = sh.getName() + "!" + sh.getDataRange().getA1Notation();

    var sql = e.parameter.SQL;

    var qry = '=query(' + rgq + ';\"' + sql + '\";1)';

    var ts = ss.insertSheet();

    var setQuery = ts.getRange(1,1).setFormula(qry);

    var getResult = ts.getDataRange().getValues();

    ss.deleteSheet(ts);

    return ContentService.createTextOutput(JSON.stringify(getResult));

  }

 

}

Thank you for your reply. I used similar code as given. The data in google sheet also similar, except it is from google form response.

This is the link to my google sheet. If you can help me..

SiSPA - Google Sheets.

Thank you.

OK, I cannot really see anything from your blocks, but it must be in there somewhere, you are adding an empty item to the beginning of the row. Have a look through your blocks update procedure and debug the row building process.

I redo everything, and now I have new problem. When update data, existing data move to the adjacent blank cell (left). End up I delete the blank column and rearrange to make sure first column must have data... ha ha. And now it works. Thank you sir for your help.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.