Esempio n. 1
0
    def write(self, google_doc_name, dictionary):
        worksheet = self._get_worksheet(google_doc_name)

        log_step("Clear spreadsheet...")
        self._clear_worksheet(worksheet)
        log_step("Writing cells...")
        start_time = time.time()

        cells = []
        row = 1
        languages = dictionary.languages
        languages = sorted(languages)
        for index, lang in enumerate(languages):
            column = index + 2
            cell = Cell((row, column))
            cell.value = lang
            cells.append(cell)

        row = 2
        for key in dictionary.keys():
            cell = Cell((row, 1))
            cell.value = key
            cells.append(cell)

            for index, lang in enumerate(languages):
                column = index + 2
                translated_value = dictionary.get_translation(key, lang)
                cell = Cell((row, column))
                cell.value = translated_value
                cells.append(cell)
            row += 1
        worksheet.update_cells(cell_list=cells)
        log_step("Done writing cells ({} seconds)".format(
            int(time.time() - start_time)))
    def update_variables(self, **kwargs):
        for var_name, var_value in kwargs.items():
            if var_name not in self.template_cells:
                continue

            template_cell = self.template_cells[var_name]
            if template_cell.direction == "fixed":
                template_cell.template_cell.value = str(var_value)
                self._cell_buffer.append(template_cell.template_cell)
            else:
                cell = Cell(tuple(template_cell.position))
                cell.value = str(var_value)

                # Update style
                if None not in template_cell.template_cell.color:
                    cell.color = template_cell.template_cell.color

                # Increase the position for the next entry of this cell
                if template_cell.direction == "row":
                    self.template_cells[var_name].inc_column()
                elif template_cell.direction == "column":
                    self.template_cells[var_name].inc_row()

                self._cell_buffer.append(cell)