0

I'm using a function (AGR_PROD) to add a row in Google Spreadsheets, and I need to count the times I use AGR_PROD function so later on I can delete the times I added these rows with another function (EraseInfo). So I have a variable called numrows to have as a counter.

var numrows = 0;
function AGR_PROD() {
   var spreadsheet = SpreadsheetApp.getActive();
   spreadsheet.getRange('12:12').activate();
   spreadsheet.getActiveSheet().insertRowsAfter(spreadsheet.getActiveRange().getLastRow(), 1);
   spreadsheet.getActiveRange().offset(spreadsheet.getActiveRange().getNumRows(), 0, 1, spreadsheet.getActiveRange().getNumColumns()).activate();
   spreadsheet.getRange('C13').activate();
   spreadsheet.getRange('C12:D12').copyTo(spreadsheet.getActiveRange(), 
   SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);

  numrows = numrows +1;
  Logger.log(numrows)
  return numrows;
};

The following code is for the erase function, so I'm trying to pass it the numrows variable.

function EraseInfo(numrows) {
   var sheet = SpreadsheetApp.getActive().getSheetByName('הצעת מחיר');
   sheet.getRange('B2:B8').clearContent();
   sheet.getRange('F1:F3').clearContent();
   Logger.log(numrows)
   sheet.deleteRows(501, numrows)
   numrows = 0;
}

The Logger is just for debugging purposes, but I get an Cannot find method deleteRows(number,(class)). (line 20, file "macros").

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Providos
  • 11
  • 3

1 Answers1

1

Since you're declaring numrows as a global variable, you don't need to pass it to the EraseInfo function. So no need to include numrows as a parameter for EraseInfo.

function EraseInfo() {
   var sheet = SpreadsheetApp.getActive().getSheetByName('הצעת מחיר');
   sheet.getRange('B2:B8').clearContent();
   sheet.getRange('F1:F3').clearContent();
   Logger.log(numrows)
   sheet.deleteRows(501, numrows)
   numrows = 0;
}

Also check out: Global variables in Google Script (spreadsheet)

You might want to use Properties Service as you can use these as a kind of persistent global variable.

u-ways
  • 6,136
  • 5
  • 31
  • 47
  • Sorry, I coppied the unmodified code, I edited the original code now. – Providos Aug 11 '18 at 16:33
  • Oh, right, good call, I had modified it, I originally put the `numrows` as parameter because the `DeleteRows` function doesn't get the amount of times I used the `AGR_PROD` function. So it displays this error: Those rows are out of bounds. (line 20, file "macros") – Providos Aug 11 '18 at 16:40
  • I don't think I can help with this new error. But you could ask this in another question with more context about the new error and what have you tried to solve this. – u-ways Aug 11 '18 at 16:56
  • Well, all I really need to know is why the `EraseInfo()` function does'nt get the `numrows` variable from the `AGR_PROD()` function. I have tried your suggestion and giving the variable `numrows` different names. I'm new to coding, I don't know what is wrong with the syntax. – Providos Aug 11 '18 at 17:25
  • 1
    > Global variables in GAS are static - you can't update them and expect them to retain their values. So you might need to use the [Properties Service](https://stackoverflow.com/a/24749707/5037430). – u-ways Aug 11 '18 at 17:37