1
votes

I'm trying to insert row via script using this code

 function insertrow() { 
      var ss = SpreadsheetApp.getActiveSpreadsheet(); 
      var ws = ss.getActiveSheet();
      var name = Browser.inputBox('Row Will Be Inserted after ID...');
      var offset=9;
      name= name+offset;
      Logger.log(name);
      ws.insertRowAfter(name); 
}

But would to insert via INPUT BOX a reference that should be offset.
E.g. I would insert row after position 18 but user should insert 9 and automatically the script has to calculate 9+9. If I try my script does 18+9=189.

How can face with? And If I want to ask to add multiple rows is there any suggestion?

1

1 Answers

1
votes

You should use parseInt() instead:

parseInt(Browser.inputBox('Row Will Be Inserted after ID...'));

to convert the input (string) to a number (integer).

Here is the updated code:

function insertrow() { 
      var ss = SpreadsheetApp.getActiveSpreadsheet(); 
      var ws = ss.getActiveSheet();
      var name = parseInt(Browser.inputBox('Row Will Be Inserted after ID...'));
      var offset=9;
      name= name+offset;
      ws.insertRowAfter(name); 
}

In order to add multiple rows, for example 10, you can replace ws.insertRowAfter(name); with :

n_rows = 10; 
ws.insertRows(name, 10);

Thus the resuting code would be:

function insertrow() { 
      var ss = SpreadsheetApp.getActiveSpreadsheet(); 
      var ws = ss.getActiveSheet();
      var name = parseInt(Browser.inputBox('Row Will Be Inserted after ID...'));
      var offset=9;
      name= name+offset;
      n_rows = 10; 
      ws.insertRows(name, 10);
}

References: