0

I've recently started writing simple functions for google sheets, and I'd like to extend the following function to all the ranges: D10:D12, H10:H12, D16:D18, H16:H18, D22:D24, H22:H24, D28:D30, H28:H30, D34:D36 and H34:H36.

What's the syntax for what will go into the getRange parenthesis?

function nextRound() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var spreadsheet = SpreadsheetApp.getActive();
 var cell = spreadsheet.getRange("D10");
 var cellValue = cell.getValue();

  cell.setValue(cellValue - 1);
}

As this function prperly works for cell D10, but if I change D10 to D10:D12, it will place the sale value from D10 -1 in the other two cells, and that's not what I want. When I run the function, I want the new value in D10 to be D10 - 1 (as it currently works), but the new value of D11 to be D11 - 1, etc.

Oh, also, if possible, I'd like to add a second function to the button to clear the data from this same range of cells when one of the cells reaches 0 or negative numbers.

Example

Rubén
  • 34,714
  • 9
  • 70
  • 166
jcodi
  • 39
  • 2
  • 10
  • In order to correctly understand your question, can you provide the samples before and after the completed script is run? And in your question, are there 2 questions? – Tanaike Jan 21 '19 at 23:02

1 Answers1

2
  1. You want to add 1 to all cells of D10:D12, H10:H12, D16:D18, H16:H18, D22:D24, H22:H24, D28:D30, H28:H30, D34:D36 and H34:H36.
  2. You want to clear the cell when the value of cell is less than 0.

If my understanding is correct, how about this answer? I used 2 methods of Values.batchGet() and Values.batchUpdate() of Sheets API for your situation, because in this case, I thought that the process cost of Sheets API is lower than that of getValues() and setValues() of SpreadsheetApp. So I think that there are several answers for your situation. So please think of this as just one of them.

When you use Sheets API, please enable Sheets API at Advanced Google Services and API console. You can see about how to enable Sheets API at here.

For question 1:

Please modify nextRound() as follows.

function nextRound() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheet = SpreadsheetApp.getActive();
  var id = ss.getId();
  var sheetName = spreadsheet.getSheetName();
  var ranges = ["D10:D12", "H10:H12", "D16:D18", "H16:H18", "D22:D24", "H22:H24", "D28:D30", "H28:H30", "D34:D36", "H34:H36"];
  ranges = ranges.map(function(e) {return sheetName + "!" + e});
  var r = Sheets.Spreadsheets.Values.batchGet(id, {ranges: ranges});
  var resource = {
    data: r.valueRanges.map(function(e) {return {
      range: e.range,
      values: e.values.map(function(f) {return [Number(f[0]) + 1]}),
    }}),
    valueInputOption: "USER_ENTERED",
  };
  Sheets.Spreadsheets.Values.batchUpdate(resource, id);
}

For question 2:

Please modify the script of "For question 1" as follows.

From:
values: e.values.map(function(f) {return [Number(f[0]) + 1]}),
To:
values: e.values.map(function(f) {return [Number(f[0]) <= 0 ? "" : Number(f[0])]}),

Note:

  • About "For question 2", About "clear" you say, I understood that you want to delete the value. If you want to put other value, please modify it.
  • If you want to run the script for "For question 2" while there are several cells with the empty value, please modify as follows.
    • values: e.values.map(function(f) {return [Number(f[0]) <= 0 ? "" : Number(f[0]) || ""]}),.

References:

If I misunderstood your question, please tell me. I would like to modify it.

Edit:

The reason of the error was due to the empty cell. So please modify as follows.

For the script of Question 1, please modify as follows.

From:
values: e.values.map(function(f) {return [Number(f[0]) + 1]}),
To:
values: e.values ? e.values.map(function(f) {return [f[0] ? Number(f[0]) + 1 : ""]}) : null,

For the script of Question 2, please modify as follows.

From:
values: e.values.map(function(f) {return [Number(f[0]) <= 0 ? "" : Number(f[0])]}),
To:
values: e.values ? e.values.map(function(f) {return [Number(f[0]) <= 0 || !f[0] ? "" : Number(f[0])]}) : null,
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you so much for your very detailed answer. I seem to have enabled the Google Sheets API, but still I get the error "ReferenceError: "Sheets" is not defined. (line 8, file "nextRoundBatch")". Is there an extra step after the API shows the status Enabled? – jcodi Jan 22 '19 at 14:58
  • @jcodi I'm really sorry for the inconvenience. In this modified script, Sheets API is used. In this case, it is required to enable Sheets API at Advanced Google Services and API console. From your reply, I thought you enabled Sheets API at API console. But you might have not enabled it at Advanced Google Services yet. By this, such error occurs. Could you please enable Sheets API at Advanced Google Services and try again? You can see how to enable it at [here](https://stackoverflow.com/a/48756509). – Tanaike Jan 22 '19 at 22:48
  • Thank you again for your time. Now I seem to have the API thing working, I mean, I guess... as now it's got past the line 8 error. However, it now displays an error saying that the method "map" in line 10 is not defined. – jcodi Jan 23 '19 at 02:28
  • @jcodi I apologize for the inconvenience. In my environment, I can confirm that the script works. When you copy and paste my script without modification, the error doesn't occur. So I would like to understand about your current situation. So can you share your a sample Spreadsheet including my script? By this, I would like to confirm your issue and resolve your issue. If you cooperate to resolve your issue, I'm glad. – Tanaike Jan 23 '19 at 05:04
  • There's nothing to apologize for. I'm super grateful for all of your help so far. Since my sheet is a complex sytem of tabs and dynamic information and it would be a lot of work to make a copy and work the referencing in the copy, I've shared the original file with your hotmail email - which I found on your Google+ profile. Do you use it? – jcodi Jan 23 '19 at 19:01
  • Btw, it's the last tab, and I already got the function I wanted working, but with a muss less sofisticate script, which took me a loong time to copy and paste all the functions manually. – jcodi Jan 23 '19 at 19:26
  • @jcodi Thank you for replying and cooperating. I could confirm your shared Spreadsheet. By this, I found the reason of the error. In your sheet, there are several empty cells in the ranges. By this, such error occurred. I modified this and updated my answer. Could you please confirm it? – Tanaike Jan 23 '19 at 22:22