python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

Feature Request: Create excel worksheets

Open keithknott26 opened this issue 1 year ago • 2 comments

Hi,

Firstly thank you for all your hard work on this project I use it often.

Would you kindly include the creation of excel worksheets into excel.py so we can get, create, update, and delete workbooks? Today I see it handles only get, update, and delete.

here's a modification to excel.py to handle this: `class WorkSheet(ApiComponent): """ An Excel WorkSheet """

_endpoints = {
    'get_tables': '/tables',
    'get_table': '/tables/{id}',
    'get_range': '/range',
    'add_table': '/tables/add',
    'get_used_range': '/usedRange(valuesOnly={})',
    'get_cell': '/cell(row={row},column={column})',
    'add_named_range': '/names/add',
    'add_named_range_f': '/names/addFormulaLocal',
    'get_named_range': '/names/{name}',
    'create_worksheet': '/worksheets'
}

# Existing constructors and methods...

def create_worksheet(self, name):
    """
    Creates a new worksheet in the workbook with the specified name.
    :param str name: The name of the new worksheet.
    :return: The newly created WorkSheet instance.
    """
    url = self.build_url(self._endpoints.get('create_worksheet'))
    data = {'name': name}
    response = self.session.post(url, json=data)
    
    if not response:
        logging.error(f"Failed to create worksheet '{name}'")
        return None
    
    return WorkSheet(parent=self.workbook, **{self._cloud_data_key: response.json()})

`

For now I'll have to work around this until it's included, appreciate your help!

keithknott26 avatar Oct 02 '24 01:10 keithknott26

I don't have the time to include this but PR are very welcome.

Thanks

alejcas avatar Oct 10 '24 14:10 alejcas

Trying to help out a bit this morning. I do not understand this request. The correct method exists in the WorkBook (where it belongs) since 2019.

Image

It works just fine in my project and creates a worksheet at the WorkBook level.

I think the requester should keep an instance of WorkBook at hand and invoke add_worksheet in their code. @keithknott26 Does this make sense to you and does that help? @alejcas Otherwise, I think this is a closed ticket.

luissantosHCIT avatar Aug 28 '25 13:08 luissantosHCIT

@alejcas Otherwise, I think this is a closed ticket.

Yeah, we can close this, thanks!

alejcas avatar Sep 02 '25 09:09 alejcas