I need some help because I have zero knowledge of Google App Script programming language and I'm not able to solve this problem; I searched the solution on different forums and I watched a lot of different videos on YouTube but I was not able to change the code as I need.
I'm combining Google Script with MIT App Inventor (this is the page from which I copied the code: https://ai2.metricrat.co.uk/guides/google-sheet-crudq-ii) and I need to change the "DELETE" function (the last one) to copy the row to delete into another sheet (called ORP_completati) in the same Google Sheet
function doGet(e) {
var ss = SpreadsheetApp.openById(e.parameter.ID);
var sh = ss.getSheetByName(e.parameter.SH);
var logSheet = ss.getSheetByName("ORP_completati");
var fn = e.parameter.FN;
var rg = sh.getDataRange().getValues();
const now = new Date();
// 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("Versione 7 mod 9");
}
}
I hope everything is clear and I want to thank you in advance for your help
CodePudding user response:
First, get the last column with content (you will need this in order to copy all the columns in the row)
var lastColumn = sh.getLastColumn()
Second, get the full row. getSheetValues takes 4 values: startRow, startColumn, number of rows and number of columns.
var copyRow = sh.getSheetValues(parseInt(index) 1, 1, 1, lastColumn)
Third, append the row to the logSheet sheet - Keep in mind that getSheetValues returns a 2D array and the appendRow function only takes a 1D array so you have to pass the first element.
logSheet.appendRow(copyRow[0])
Finally, you can delete the row
sh.deleteRow(parseInt(index) 1);
Resources: https://developers.google.com/apps-script/reference/spreadsheet/sheet#getSheetValues(Integer,Integer,Integer,Integer) https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendrowrowcontents