예제 #1
0
    def set_json(self, cell_data):
        """
        Reads a json-dictionary returned by the Google Sheets API v4 and initialize all the properties from it.

        :param cell_data:   The cells data.
        """
        self._simplecell = False

        self._value = cell_data.get('formattedValue', '')
        try:
            self._unformated_value = list(cell_data['effectiveValue'].values())[0]
        except (KeyError, IndexError):
            self._unformated_value = ''
        self._formula = cell_data.get('userEnteredValue', {}).get('formulaValue', '')

        self._note = cell_data.get('note', None)
        nformat = cell_data.get('userEnteredFormat', {}).get('numberFormat', {})
        self.format = (nformat.get('type', None), nformat.get('pattern', ''))
        color = cell_data.get('userEnteredFormat', {}) \
            .get('backgroundColor', {'red': None, 'green': None, 'blue': None, 'alpha': None})

        self._color = (color.get('red', 0), color.get('green', 0), color.get('blue', 0), color.get('alpha', 0))
        self.text_format = cell_data.get('userEnteredFormat', {}).get('textFormat', None)
        if self.text_format and self.text_format.get('foregroundColor', None):
            self.text_format['foregroundColor'] = format_color(self.text_format['foregroundColor'], to='tuple')
        self.text_rotation = cell_data.get('userEnteredFormat', {}).get('textRotation', None)
        self.borders = cell_data.get('userEnteredFormat', {}).get('borders', None)
        self._wrap_strategy = cell_data.get('userEnteredFormat', {}).get('wrapStrategy', "WRAP_STRATEGY_UNSPECIFIED")

        nhorozondal_alignment = cell_data.get('userEnteredFormat', {}).get('horizontalAlignment', None)
        self._horizontal_alignment = \
            HorizontalAlignment[nhorozondal_alignment] if nhorozondal_alignment is not None else None
        nvertical_alignment = cell_data.get('userEnteredFormat', {}).get('verticalAlignment', None)
        self._vertical_alignment = \
            VerticalAlignment[nvertical_alignment] if nvertical_alignment is not None else None
예제 #2
0
    def get_json(self):
        """Returns the cell as a dictionary structured like the Google Sheets API v4."""
        try:
            nformat, pattern = self.format
        except TypeError:
            nformat, pattern = self.format, ""

        if self._formula != '':
            value = self._formula
            value_key = 'formulaValue'
        elif is_number(self._value):
            value = self._value
            value_key = 'numberValue'
        elif type(self._value) is bool:
            value = self._value
            value_key = 'boolValue'
        elif type(self._value) is str or type(self._value) is unicode:
            value = self._value
            value_key = 'stringValue'
        else:   # @TODO errorValue key not handled
            value = self._value
            value_key = 'errorValue'

        ret_json = dict()
        ret_json["userEnteredFormat"] = dict()

        if self.format[0] is not None:
            ret_json["userEnteredFormat"]["numberFormat"] = {"type": getattr(nformat, 'value', nformat),
                                                             "pattern": pattern}
        if self._color[0] is not None:
            ret_json["userEnteredFormat"]["backgroundColor"] = {"red": self._color[0], "green": self._color[1],
                                                                "blue": self._color[2], "alpha": self._color[3]}
        if self.text_format is not None:
            ret_json["userEnteredFormat"]["textFormat"] = self.text_format.copy()
            fg = ret_json["userEnteredFormat"]["textFormat"].get('foregroundColor', None)
            ret_json["userEnteredFormat"]["textFormat"]['foregroundColor'] = format_color(fg, to='dict')

        if self.borders is not None:
            ret_json["userEnteredFormat"]["borders"] = self.borders
        if self._horizontal_alignment is not None:
            ret_json["userEnteredFormat"]["horizontalAlignment"] = self._horizontal_alignment.value
        if self._vertical_alignment is not None:
            ret_json["userEnteredFormat"]["verticalAlignment"] = self._vertical_alignment.value
        if self._wrap_strategy is not None:
            ret_json["userEnteredFormat"]["wrapStrategy"] = self._wrap_strategy
        if self.text_rotation is not None:
            ret_json["userEnteredFormat"]["textRotation"] = self.text_rotation

        if self._note is not None:
            ret_json["note"] = self._note
        ret_json["userEnteredValue"] = {value_key: value}

        return ret_json