Fields

Contains a set of `https://developers.google.com/apps-script/reference/data-studio/field`s for a community connector. This set of fields define which
dimensions and metrics can be used in Data Studio.

```javascript
const cc = DataStudioApp.createCommunityConnector();
const fields = cc.getFields();

const field1 = fields
                   .newDimension()
                   // Set other properties as needed.
                   .setId('field1_id');
```

### Methods

| Method | Return type | Brief description |
|---|---|---|
| `https://developers.google.com/apps-script/reference/data-studio/fields#asArray()` | `https://developers.google.com/apps-script/reference/data-studio/field` | Returns a view of this object as an array. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#build()` | `Object[]` | Validates this object and returns it in the format needed by Data Studio. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#forIds(String)` | `https://developers.google.com/apps-script/reference/data-studio/fields#` | Returns a new `https://developers.google.com/apps-script/reference/data-studio/fields#` object filtered to `https://developers.google.com/apps-script/reference/data-studio/field`s with an ID in `ids`. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#getDefaultDimension()` | `https://developers.google.com/apps-script/reference/data-studio/field` | Returns the default dimension to be used for the set of fields. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#getDefaultMetric()` | `https://developers.google.com/apps-script/reference/data-studio/field` | Returns the default metric to be used for the set of fields. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#getFieldById(String)` | `https://developers.google.com/apps-script/reference/data-studio/field` | Returns a field with a given ID, or `null` if no field with that ID is in this `https://developers.google.com/apps-script/reference/data-studio/fields#` object. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#newDimension()` | `https://developers.google.com/apps-script/reference/data-studio/field` | Returns a new dimension `https://developers.google.com/apps-script/reference/data-studio/field`. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#newMetric()` | `https://developers.google.com/apps-script/reference/data-studio/field` | Returns a new metric `https://developers.google.com/apps-script/reference/data-studio/field`. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#setDefaultDimension(String)` | `void` | Sets the default dimension to be used for the set of fields. |
| `https://developers.google.com/apps-script/reference/data-studio/fields#setDefaultMetric(String)` | `void` | Sets the default metric to be used for the set of fields. |

## Detailed documentation

### `asArray()`

Returns a view of this object as an array.

```javascript
const fields = DataStudioApp.createCommunityConnector().getFields();
fields.newDimension().setId('field1_id');
fields.newDimension().setId('field2_id');
fields.newDimension().setId('field3_id');

// Logging the ID of each field:
fields.asArray().map((field) => {
  Logger.log(field.getId());
});
```

#### Return


`https://developers.google.com/apps-script/reference/data-studio/field` --- An array of `https://developers.google.com/apps-script/reference/data-studio/field` objects.

*** ** * ** ***

### `build()`

Validates this object and returns it in the format needed by Data Studio.

#### Return


`Object[]` --- The validated field data, represented as a list of objects.

#### Throws


[`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) --- if a valid object cannot be constructed.

*** ** * ** ***

### `forIds(ids)`

Returns a new `https://developers.google.com/apps-script/reference/data-studio/fields#` object filtered to `https://developers.google.com/apps-script/reference/data-studio/field`s with an ID in `ids`.

```javascript
const fields = DataStudioApp.createCommunityConnector().getFields();
fields.newDimension().setId('field1_id');
fields.newDimension().setId('field2_id');
fields.newDimension().setId('field3_id');

// subsetFields is a Fields object that only contains field1 and field2.
const subsetFields = fields.forIds(['field1_id', 'field3_id']);
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `ids` | `String[]` | The IDs of fields that should be kept. |

#### Return


`https://developers.google.com/apps-script/reference/data-studio/fields#` --- A new `https://developers.google.com/apps-script/reference/data-studio/fields#` object.

*** ** * ** ***

### `getDefaultDimension()`

Returns the default dimension to be used for the set of fields. The default dimension is
selected automatically when a new visualization is made.

#### Return


`https://developers.google.com/apps-script/reference/data-studio/field` --- The dimension that is set as the default dimension, or `null` if no default
dimension was set.

*** ** * ** ***

### `getDefaultMetric()`

Returns the default metric to be used for the set of fields. The default metric is selected
automatically when a new visualization is made.

#### Return


`https://developers.google.com/apps-script/reference/data-studio/field` --- The metric that is set as the default metric, or `null` if no default metric was
set.

*** ** * ** ***

### `getFieldById(fieldId)`

Returns a field with a given ID, or `null` if no field with that ID is in this `https://developers.google.com/apps-script/reference/data-studio/fields#` object.

```javascript
const fields = DataStudioApp.createCommunityConnector().getFields();
const field1 = fields.newDimension().setId('field1_id');

// byId is the same as field1.
const byId = fields.getFieldById('field1_id');

// byId2 is null.
const byId2 = fields.getFieldById('not present id');
```

#### Parameters

| Name | Type | Description |
|---|---|---|
| `fieldId` | `String` | The ID of the field to get. |

#### Return


`https://developers.google.com/apps-script/reference/data-studio/field` --- The `https://developers.google.com/apps-script/reference/data-studio/field` with the given ID, or `null` if no such field is contained
within this `https://developers.google.com/apps-script/reference/data-studio/fields#` object.

*** ** * ** ***

### `newDimension()`

Returns a new dimension `https://developers.google.com/apps-script/reference/data-studio/field`.

#### Return


`https://developers.google.com/apps-script/reference/data-studio/field` --- A new dimension `https://developers.google.com/apps-script/reference/data-studio/field`.

*** ** * ** ***

### `newMetric()`

Returns a new metric `https://developers.google.com/apps-script/reference/data-studio/field`.

#### Return


`https://developers.google.com/apps-script/reference/data-studio/field` --- A new metric `https://developers.google.com/apps-script/reference/data-studio/field`.

*** ** * ** ***

### `setDefaultDimension(fieldId)`

Sets the default dimension to be used for the set of fields. The default dimension is selected
automatically when a new visualization is made.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `fieldId` | `String` | The ID of the field to use as the default dimension. This ID must be present in the set of fields. |

*** ** * ** ***

### `setDefaultMetric(fieldId)`

Sets the default metric to be used for the set of fields. The default metric is selected
automatically when a new visualization is made.

#### Parameters

| Name | Type | Description |
|---|---|---|
| `fieldId` | `String` | The ID of the field to use as the default metric. This ID must be present in the set of fields. |