- You want to search and retrieve DeveloperMetadata from rows and columns using Class DeveloperMetadata.
- You want the sample scripts for above situation.
If my understanding is correct, how about this sample script? I tried to think of about this situation because this is also useful for my situation. I think that there are several sample scripts for your situation. So please think of this as one of them.
Sample script 1:
In this sample script, all DeveloperMetadata is retrieved from the sheet which has the sheet name of "Sheet1".
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var v = s.getRange(1, 1, s.getMaxRows(), s.getMaxColumns()).createDeveloperMetadataFinder().onIntersectingLocations().find();
var res = v.map(function(e) {
var loc = e.getLocation();
var obj = loc.getLocationType() === SpreadsheetApp.DeveloperMetadataLocationType.COLUMN ? {range: loc.getColumn().getA1Notation()} :
loc.getLocationType() === SpreadsheetApp.DeveloperMetadataLocationType.ROW ? {range: loc.getRow().getA1Notation()} : {};
obj[e.getKey()] = e.getValue();
return obj;
});
Logger.log(res)
- As a sample situation, after DeveloperMetadata is set to the column "A", when it is moved to the column "B", this script retrieves the key and values of column "B".
- For example, if you want to search using key and value, you can also use the following scripts.
var v = s.getRange(1, 1, s.getMaxRows(), s.getMaxColumns()).createDeveloperMetadataFinder().onIntersectingLocations().withKey("key1").find();
var v = s.getRange(1, 1, s.getMaxRows(), s.getMaxColumns()).createDeveloperMetadataFinder().onIntersectingLocations().withValue("value").find();
var v = s.getRange(1, 1, s.getMaxRows(), s.getMaxColumns()).createDeveloperMetadataFinder().onIntersectingLocations().withKey("key1").withValue("value1").find();
- This is a sample script. So please modify this for your situation.
Result:
When 2 DeveloperMetadata of {key1: "value1"}
and {key2: "value2"}
are set to row 1 and column A, the following result is obtained.
[
{
"range": "1:1",
"type": "ROW",
"key1": "value1"
},
{
"range": "A:A",
"type": "COLUMN",
"key2": "value2"
}
]
Sample script 2:
As another way, you can also search DeveloperMetadata using Sheets API as follows. 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.
var resource = {"dataFilters": [{"developerMetadataLookup": {"metadataKey": "key1"}}]}; // Search by key
// var resource = {"dataFilters": [{"developerMetadataLookup": {"metadataValue": "value1"}}]}; // Search by value
// var resource = {"dataFilters": [{"developerMetadataLookup": {"metadataKey": "key1", "metadataValue": "value1"}}]}; // Search by key and value
var res = Sheets.Spreadsheets.DeveloperMetadata.search(resource, spreadsheetId);
References:
If this was not what you want, I'm sorry.
Edit:
For your 2nd question.
- Add a metadata to "H:H" as the key of "columnName" and the value of "price".
- Envn if the column "H" was moved, you want to retrieve the new range using the value of metadata.
If my understanding is correct, how about this?
Add metadata
In order to add a metadata to the column "H", you can use the following script. Because there is no method of setMatadata()
, please use addDeveloperMetadata()
.
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("H:H").addDeveloperMetadata("columnName", "price");
Retrieve ranges using value
This sample script retrieves ranges using a value of metadata. In this script, envn if the column "H" was moved, the new range using the value of metadata can be retrieved.
var sheet = SpreadsheetApp.getActiveSheet();
var value = "price";
var v = sheet.getRange(1, 1, 1, sheet.getMaxColumns())
.createDeveloperMetadataFinder()
.onIntersectingLocations()
.withValue(value)
.find();
var ranges = v.map(function(e) {return e.getLocation().getColumn()}); // "ranges" is one dimensional array.