Update/edit google sheets row values from html input

有些话、适合烂在心里 提交于 2020-06-16 04:40:07

问题


I have an html form where the client's data is inserted in and it appends row with the values on to a google sheet.

In the form, there's a field that searches and returns the clients data when searching for a specific value (id number).

function getID(IDsearch){
  var ws = SpreadsheetApp.getActiveSheet();
  var data = ws.getRange(3, 1, ws.getLastRow(), 36).getValues();

  var dataInput = data.map(function(r){return r[7];});  
  var position = dataInput.indexOf(IDsearch);
  var dataArray = ws.getRange(position+3, 1, 1, 36).getValues();

  if(position > -1){
    return dataArray;
  } else {
    return position;
  }

} 

After this runs, all the input fields in the form are populated with the data from that row. I need to edit the values in the form and when submit it should overwrite/update the existing row with that id number.

In google sheets documentation, I've found the spreadsheets.values.update method, but I cannot figure this out. I'm pretty new in this and any help would be appreciated.

Thanks everyone!


回答1:


  • You want to achieve the following flow.
    1. Input "ID" to id="insertID" and click "Search by ID".
    2. Show the values from Spreadsheet by searching "ID".
    3. Edit the values of id="name" and id="ID".
    4. When "Save data" is clicked, you want to update the values on the Spreadsheet.

From your replying, shared Spreadsheet and script, I could understand like above. If my understanding is correct, how about ths following modification? Please think of this as just one of several possible answers.

Modification points:

  • In your case, processForm at Google Apps Script side is required to be modified.
    • Search the row using formObject and overwrite the values of cells.

Modified script:

When your script is modified, please modify processForm at Google Apps Script side as follows. I remove the Spreadsheet ID from the URL. So please set it, before you test the script.

function processForm(formObject) {
  var url = "https://docs.google.com/spreadsheets/d/###/edit#gid=0";
  var ss = SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("Database");

  // I added and modified below script.
  var ranges = ws.getRange(4, 2, ws.getLastRow() - 3, 1).createTextFinder(formObject.ID).findAll();
  if (ranges.length > 0) {
    for (var i = 0; i < ranges.length; i++) {
      ranges[i].offset(0, -1, 1, 2).setValues([[formObject.name, formObject.ID]]);
    }
  } else {
    ws.appendRow([formObject.name, formObject.ID]);
  }
}
  • In this modification, when the same IDs are existing, all rows of the same IDs are overwritten. For example, if you want to modify the 1st one, please modify to ranges[0].offset(0, -1, 1, 2).setValues([[formObject.name, formObject.ID]]);.

Reference:

  • Class TextFinder



回答2:


Try this:

function getID(IDsearch){
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();//dont know what the sheet is
  var rg=sh.getRange(3,1,sh.getLastRow()-2,36);
  var data=rg.getValues();
  var idA=sh.getRange(3,8,sh.getLastRow()-2,1).getValues().map(function(r){return r[0];});//it looked like column 8 was your id column
  var idx=idA.indexOf(IDsearch);
  if(idx>-1) {
    return ws.getRange(pos + 3,1,1,36).getValues()[0];//flattened the row to a 1d array
  }else{
    return idx;
  }
}



回答3:


@dianadfonseca, as @Tanaike points out, without more detail about your data structure, people will be speculating in order to answer your question. As I will be...

Please read the following answer, and tailor it to your needs if it works for you.

Example:

function getRow(id){

  var ws = SpreadsheetApp.getActiveSheet();

  // Number of headers to skip
  var numHeaders = 2;

  // the starting row
  var startRow = numHeaders + 1;

  // The column where the IDs are is known
  var idCol = 8;

  // The number of rows with data not headers
  var numRows = ws.getDataRange().getLastRow() - numHeaders;

  // An array with the ids to find a match in
  // getRange() returns a 2D array, so you can transpose it to flatten it
  var ids = ws.getRange(startRow,idCol,numRows).getValues();
  ids = transpose(ids)[0];

  // Get the index where id matches in ids
  var row = ids.indexOf(id);

  // If there's a match
  if(row > -1){

    // Correct row indexing 
    row = row + startRow;
  } 

  return row;
}

function updateRow(row,data){
  var ws = SpreadsheetApp.getActiveSheet();

  // The column for each property is known
  var propertyOneCol = 1;

  // Update property using setValue()
  ws.getRange(row,propertyOneCol).setValue(data.propertyOne);

  // And so on...

}

// Transpose to avoid looping through the array
function transpose(a)
{
  return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}

You can take a look the spreadsheet used for this example here with its bound script to play around.

Here is the function I used for testing

function test(){

  // You are receiving this from your form
  var data = {"propertyOne":"Juan","propertyTwo":20, "id":123467};
  var id = data.id;
  updateRow(getRow(id),data);
}


来源:https://stackoverflow.com/questions/60031293/update-edit-google-sheets-row-values-from-html-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!