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.
Invoke-RestMethod -Method PatchtoInvoke-RestMethod -Method Putand let me know the response you are getting