2

I have registered app in Azure and I have it's clientId and clientSecret for authentication.

I want to be able by Powershell to write into Excel online file cell. The App registration should have enough API permission ( Files.Read.All , Files.ReadWrite.All , Sites.Manage.All , Sites.Read.All , Sites.ReadWrite.All and it has Admin consent ) to write into Excel Online file stored in Teams channel and it Has edit permision in the file settings itself.

In powershell I can get Excel online file and I can get value of one cell but I am not able to write into the cell because when I try do the writing part I am getting this error: Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

for this line:

 $response = Invoke-RestMethod -Method Patch -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/items/$fileId/workbook/worksheets/$sheetName/range(address='B1')" -Headers @{ Authorization = "Bearer $accessToken" } -Body ($updateBody | ConvertTo-Json) -ContentType "application/json"

Can someone please help me how to do the writing into excel Online file's cell can be done?

There is the whole Powershell script:



# --- Configuration ---

$tenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$clientId = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
$clientSecret = "qqqqq~qqqqqqqqqqqqqqqq.qqqqq_qqqqqqqqqqq"
$teamId = "zzzzzzzz-zz15-4zzz-9zzz-eazzzzzzzzzz" # Extracted from the team link
$channelId = "19:[email protected]" # Extracted from the channel link
$excelFileName = "Information_Point.xlsx"
$worksheetName = "NewHireLog"


# Get Access Token
$body = @{
     grant_type = "client_credentials"
     client_id = $clientId
     client_secret = $clientSecret
     resource = "https://graph.microsoft.com"
}

$response = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" -ContentType "application/x-www-form-urlencoded" -Body $body

$accessToken = $response.access_token


# Get Files Folder ID

$response = Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels/$channelId/filesFolder" -

Headers @{ Authorization = "Bearer $accessToken" }

$driveId = $response.parentReference.driveId
$folderId = $response.id



# List Files in the Folder
 $response = Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/items/$folderId/children" -Headers @{ Authorization = "Bearer $accessToken" }
 
 $files = $response.value
 
 $file = $files | Where-Object { $_.name -eq $fileName }
 
 # Get the value of cell B1
 
  $response = Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/items/$fileId/workbook/worksheets/$sheetName/range(address='B1')" -Headers @{ Authorization = "Bearer $accessToken" }
  
  $cellValue = $response.values[0][0]
  
  Write-Output "Current value of B1: $cellValue"
  
  # Update the value of cell B1 to "Changed Value"
  
  
# Update the value of cell B1 to "Changed Value"

$updateBody = @{
    values = @(
    @("Changed Value")
    )
}



$response = Invoke-RestMethod -Method Patch -Uri "https://graph.microsoft.com/v1.0/drives/$driveId/items/$fileId/workbook/worksheets/$sheetName/range(address='B1')" -Headers @{ Authorization = "Bearer $accessToken" } -Body ($updateBody | ConvertTo-Json) -ContentType "application/json"

# It return this Error: Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

# Output the response
$response


I am able to access Excel Online file and the sheet and the cell value but I am not able to wtite into cell a new value.

5
  • Could you replace the Invoke-RestMethod -Method Patch to Invoke-RestMethod -Method Put and let me know the response you are getting Commented Apr 16 at 13:00
  • @PratikJadhav the MS documentation for this endpoints doesn't list PUT as a supported Verb (only GET/POST/PATCH/DELETE are supported) Commented Apr 16 at 13:25
  • When I try Invoke-RestMethod -Method Put Then I got this error: Invoke-RestMethod : The remote server returned an error: (405) Method Not Allowed. Commented Apr 16 at 13:45
  • @Jazzdave Hope your issue is resolved? Commented Apr 17 at 6:13
  • @PratikJadhav Yes, it is. it works now, but I would need now help how should looks like the $updateBody and the request Invoke-RestMethod line when I would need edit whole row and not just one cell. If someone has an idea it would be great. Commented Apr 17 at 7:09

1 Answer 1

2

I would try to define values as array of arrays

$updateBody = @{
    values = @(
    ,@("Changed Value")
    )
}

Your $updateBody is missing comma , before @("Changed Value")

$updateBody = @{
    values = @(
    @("Changed Value")
    )
}
Sign up to request clarification or add additional context in comments.

1 Comment

It helped. Thank you, it works now. Can you please advise me how to update whole row ? How should the $updateBody looks like?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.