How to duplicate a worksheet with its content with microsoft graph excel api

649 Views Asked by At

i'm trying to duplicate a worksheet with its content into another new one.

In origin what i have is a table and i want to copy the worksheet with the table and its content.

In programming in PHP and i'm trying this:

$sheet = $graph->createRequest('GET', $originSheet)
        ->setReturnType(Model\WorkbookWorksheet::class)
        ->execute();

    $sheet->setName($worksheetName);

    
    $graph->createRequest('POST', $sheetUrl)
        ->setReturnType(Model\WorkbookWorksheet::class)
        ->attachBody($sheet)
        ->execute();

But the content is not duplicated, i just get a duplicated empty worksheet.

I tried to perform a GET to retrieve the origin table of origin worksheet, something like this:

$originSheetTables = $graph->createRequest('GET', $originSheetUrl)
        ->setReturnType(Model\WorkbookTable::class)
        ->execute();

But i don't know how to use the response of this request ($originSheetTables) to add into the new worksheet. I have tried this request with this approach:

$graph->createRequest('POST', $sheetUrl.'/'.$worksheetName.'/tables/add')
        ->attachBody($originSheetTables[0])          
        ->execute();

But i'm getting a 400 error: "The request URI is not valid. The segment 'add' must be the (truncated...)"

Could you help me with this?

Thanks in advance.

1

There are 1 best solutions below

0
On

I don't think you can directly duplicate the whole worksheet with data in a single call but you can try a work around by first creating a worksheet and later add a table and then get data from the previous worksheet table
and then add the values as rows to the second table in second spread sheet.

If you want to have it done on a single call, you can raise a uservoice on the Microsoft Graph Uservoice Forum for this feature.