initializeSheet()

in marketing-analytics/activation/sheets-based-installer/src/apps_script/2_base/mojo.js [259:358]


  initializeSheet() {
    // Clear sheet and create headline
    this.enhancedSheet.initialize(MOJO_SHEET_CONFIG, this.headlineStyle);

    const { sheet } = this.enhancedSheet;
    // Mojo sheet last column index
    const numColumns = MOJO_FIELDS.length;
    const valueCellIndex = MOJO_FIELDS.indexOf('value') + 1;
    const enableCellIndex = MOJO_FIELDS.indexOf('enable') + 1;
    /**
     * A map for properties and their A1 notations. This is used to replace
     * values that have placeholders like `${propertyName}`.
     */
    const propertyA1s = {};
    // The whole category will be enabed or disabled as a group.
    this.groupedCategory = {};
    /**
     * Returns an array that stands for a row data in sheet from a MojoResource.
     * @param {!MojoResource} row
     * @param {number} index
     * @return {!Array<string>}
     */
    const generateSheetRow = (row, index) => {
      const result = [];
      const rowIndex = index + ROW_INDEX_SHIFT;
      // 'value' cell format & value
      const valueCell = sheet.getRange(rowIndex, valueCellIndex, 1, 1);
      this.setValueCell_(valueCell, row.editType);
      Object.keys(row).forEach((key) => {
        if (typeof row[key] === 'string' && row[key].indexOf('${') > -1) {
          // Update place holders with sheet functions
          row[key] = this.replacePlaceHolder_(row[key], propertyA1s);
        }
      });
      // 'enable' cell format & value
      const enableCell = sheet.getRange(rowIndex, enableCellIndex, 1, 1);
      const { group, optionalType = OPTIONAL_TYPE.MANDATORY } = row;
      row.enable = this.initializeEnableCell_(enableCell, group, optionalType);
      if (row.propertyName) {
        if (propertyA1s[row.propertyName]) {
          console.warn(`Property '${row.propertyName}' has A1 notation `,
            propertyA1s[row.propertyName], ', will ignore the new A1 notation',
            valueCell.getA1Notation());
        } else {
          propertyA1s[row.propertyName] = valueCell.getA1Notation();
        }
      }
      // Prepare data array from the row
      Object.keys(row).forEach((key) => {
        const value = row[key];
        const index = MOJO_FIELDS.indexOf(key);
        if (index > -1) {
          result[index] = typeof value === 'function' ? value(row) : value;
        }
        if (key.endsWith('datarange')) { //Set a droplist for the cell.
          const columnName = key.split('_')[0];
          this.setDropList_(columnName, rowIndex, value);
        }
      });
      // Prepare links for the row
      Object.keys(row)
        .filter((key) => key.endsWith('link'))
        .forEach((key) => {
          const columnName = key.split('_')[0];
          const index = MOJO_FIELDS.indexOf(columnName);
          const url = row[key].startsWith('=')
            ? row[key].substring(1)
            : `"${row[key]}"`;
          result[index] = `=HYPERLINK(${url}, "${result[index]}")`;
        });
      // Fill the last column of this row
      if (!result[numColumns - 1]) result[numColumns - 1] = '';
      return result;
    };

    const flatConfig = this.flattenValue_(); // Flatten MojoResources
    const sheetRows = flatConfig.map(generateSheetRow); // Map to sheet data

    // Set content and style.
    sheet.getRange(ROW_INDEX_SHIFT, 1, flatConfig.length, numColumns)
      .setValues(sheetRows)
      .setWrapStrategy(SpreadsheetApp.WrapStrategy.WRAP)
      .setBorder(true, true, true, true, true, true, '#9AA0A6'
        , SpreadsheetApp.BorderStyle.SOLID);

    this.enhancedSheet.mergeVertically();

    // Status column conditional format
    const statusIndex = MOJO_FIELDS.indexOf('status') + 1;
    const statusColumn = sheet.getRange(
      ROW_INDEX_SHIFT, statusIndex, sheet.getMaxRows(), 1);
    const statusRules = Object.keys(STATUS_BACKGROUND_COLOR).map(
      (key) => {
        return SpreadsheetApp.newConditionalFormatRule()
          .whenTextEqualTo(key).setBackground(STATUS_BACKGROUND_COLOR[key])
          .setRanges([statusColumn]).build();
      }
    );
    sheet.setConditionalFormatRules(statusRules);
  }