I want to get A1Notation of rectangle areas on spreadsheet. Sample of rectangle on spreadsheet is below image.
I want to get A1Notation of cells of value "1". I don't want to get "B2, C2, D2, B3, C3, D3, B4, C4, D4". I want to get "B2:D4". My tried script is
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var result = [];
for (var i = 0; i < data.length; i++){
for (var j = 0; j < data[i].length; j++){
if (data[i][j] == 1) {
var range = sheet.getRange(i + 1, j + 1).getA1Notation();
result.push(range);
}
}
}
Logger.log(result);
This script returns "B2, C2, D2, B3, C3, D3, B4, C4, D4". sheet.getDataRange().getA1Notation()
is "A1:D4". Positions of cells of "1" are not always the same. My question is
- When positions of cells of "1" make a rectangle area, are there any ways to retrieve the rectangle area as A1Notation?
- At sample, I want to get "B2:D4" from spreadsheet or from "B2, C2, D2, B3, C3, D3, B4, C4, D4".
I checked Puzzle: Find largest rectangle (maximal rectangle problem) and links. But I cant understand the way to apply this to my script. Thank you so much for your time.
Edit
These are samples. I get areas in size order.
A B C D E
1 0 0 0 0 0
2 0 1 1 1 0
3 0 1 1 1 0
4 0 1 1 1 0
5 0 0 0 0 0
I need "B2:D4".
A B C D E
1 1 1 1 0 0
2 0 1 1 1 0
3 0 1 1 1 0
4 0 1 1 1 0
5 0 0 0 0 0
I need "B2:D4" and "A1:C1".
A B C D E
1 0 1 1 1 1
2 0 1 1 1 1
3 0 0 0 1 1
4 0 0 0 0 1
5 0 0 0 0 1
I need "B1:E2", "E3:E5", "D3:D3".