Registrei o aplicativo no Azure e tenho seu clientId e clientSecret para autenticação.
Quero poder usar o PowerShell para gravar em uma célula de arquivo do Excel Online. O registro do aplicativo deve ter permissão de API suficiente (Files.Read.All, Files.ReadWrite.All, Sites.Manage.All, Sites.Read.All, Sites.ReadWrite.All e consentimento do administrador) para gravar em um arquivo do Excel Online armazenado no canal do Teams, e deve ter permissão de edição nas próprias configurações do arquivo.
No PowerShell, consigo obter um arquivo do Excel online e obter o valor de uma célula, mas não consigo escrever na célula porque, quando tento fazer a parte de escrita, recebo este erro: Invoke-RestMethod: O servidor remoto retornou um erro: (400) Solicitação inválida.
para esta linha:
$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"
Alguém pode me ajudar como fazer a escrita em uma célula de arquivo do Excel Online?
Aqui está o script completo do Powershell:
# --- 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
Consigo acessar o arquivo do Excel Online, a planilha e o valor da célula, mas não consigo escrever um novo valor na célula.
Eu tentaria definir
values
como uma matriz de matrizesEstá
$updateBody
faltando uma vírgula,
antes@("Changed Value")