【问题标题】:How to open a SharePoint-List with Excel-VBA如何使用 Excel-VBA 打开 SharePoint 列表
【发布时间】:2021-04-28 21:13:29
【问题描述】:
我有以下想法!
-
首先,将我的 Excel 与 SharePoint 连接,以获取 SharePoint-List。
-
其次,遍历列表中的每一项,并一一打开。
-
在每个列表项中都有一个包含不同文件的库。如Word、Excel等。
-
我需要计算列表中有多少列表项以及每个列表项中有多少文件。
如果这有效,那么...
-
在 SharePoint 的列表项中打开正确的 Excel 文件。类似 '%activities% 的 excel 文件名。
-
计算此 Excel 文件中的行数并将数据复制到新的 Excel 文件中。
所以最后我们会得到一个新的 Excel 文件,其中包含来自不同 Excel 文件的几张工作表。这甚至可能吗?还是这个想法太疯狂了:)
我尝试这样连接:
Sub test()
Dim dm As New DriveMapper
Dim sharepointFolder As Scripting.folder
Set sharepointFolder = dm.MapDrive("\\***.sharepoint.com@SSL\sites\General1462\Lists\")
Debug.Print sharepointFolder.Path
End Sub
但我收到了错误代码 (800704dc)。我试过这个解决方案Get the content of a sharepoint folder with Excel VBA
【问题讨论】:
标签:
excel
vba
sharepoint
count
office365
【解决方案1】:
Function tableWithAllSub([Microsoft.SharePoint.Client.Web]$Web){
$counterSub= 1
$counterLoadingBar = 0
$counterForFileFound = 0
$counterFileIsEmpty = 0
$counterFolderIsEmpty = 0
$counterForSumupAllRows = 0
$counterForTemplates = 0
$excludedLists = @("Reusable Content","Content and Structure Reports","Form Templates")
$Lists = Get-PnPList | Where {$_.Hidden -eq $False -and $excludedLists -notcontains $_.Title}
$xl = new-object -c excel.application
$xl.displayAlerts = $false
$tableAllInformation = foreach ($List in $Lists){
$fileFound = "Nein"
$counterAllItemsElseIf = 0
#FileRef = Pfad zur Datei. File_x0020_Type = Dateityp. FileLeafRef = Name der Datei.
$allItems = Get-PnPListItem -List $List -Fields "FileRef", "File_x0020_Type", "FileLeafRef"
foreach ($Item in $allItems) {
$counterOfRows = 0
$counterAllItemsElseIf++
if ($Item["FileLeafRef"] -like "*Record*") {
$fileFound = "Ja"
$counterForFileFound++
Get-PnPFile -Url $Item["FileRef"] -Path C:\Users\$env:USERNAME\Desktop\$nameOfDirectoryToday\tmp\ -Filename $Item["FileLeafRef"] -AsFile -Force
$nameOfFile = $Item["FileLeafRef"]
$filePath = "C:\Users\$env:USERNAME\Desktop\$nameOfDirectoryToday\tmp\$nameOfFile"
$row = 4
$column = "C"
$excelDocumentSource = $xl.workbooks.open($filePath, $null, $true)
$sheetToSearchIn = $excelDocumentSource.sheets.item('REGISTER')
$cellEmpty = "Nein"
if([string]::IsNullOrEmpty($sheetToSearchIn.Cells.Item($row, $column).Value2) ) {
$cellEmpty = "Ja"
$counterFileIsEmpty++
}
while(-not ([string]::IsNullOrEmpty($sheetToSearchIn.Cells.Item($row + $counterOfRows, $column).Value2) ) ) {
$counterOfRows++
}
$counterForSumupAllRows += $counterOfRows
if($counterOfRows -eq 36 -and $sheetToSearchIn.Cells.Item(4, "C").Value2 -eq "services" -and $sheetToSearchIn.Cells.Item(5, "C").Value2 -eq "Local"){
$cellEmpty = "Template"
$counterForTemplates++
}
$excelDocumentSource.close($false)
$userValueCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$Item["Editor"]
$lastModifiedBy = $userValueCollection.LookupValue.ToString()
$modifiedOn = $Item["Modified"]
if($List.Title -eq "service"){
$subFolderService = searchForForwardSlash($Item["FileRef"].substring(29))
$nameofSub = $List.Title + " - " + $subFolderService
}
else{$nameofSub = $List.Title}
[PSCustomObject] @{
'N' = $counterSub++
'T' = $nameofSub
'D' = $fileFound
'Z' = $cellEmpty
'A' = $counterOfRows
'Z' = $lastModifiedBy
'G' = $modifiedOn
'D' = $Item["FileLeafRef"]
'P' = $Item["FileRef"]
}
}
elseIf ($counterAllItemsElseIf -eq $allItems.count -and $fileFound -eq "Nein"){
$counterFolderIsEmpty++
if($List.Title -eq "service"){
$subFolderService = searchForForwardSlash($Item["FileRef"].substring(29))
$nameofSub = $List.Title + " - " + $subFolderService
}
else{$nameofSub = $List.Title}
[PSCustomObject] @{
'N' = $counterSub++
'T' = $nameofSub
'D' = $fileFound
'Z' = " "
'A' = " "
'Z' = " "
'G' = " "
'D' = " "
'P' = " "
}
}
}
$counterLoadingBar++
$showProgress = [int]( (100/$Lists.count) * $counterLoadingBar)
Write-Progress -Activity "Search in Progress" -Status "$showProgress% Complete:" -PercentComplete ( (100/$Lists.count) * $counterLoadingBar)
}
$endResult= [PSCustomObject] @{
'A' = $Lists.count
'O' = $counterFolderIsEmpty
'A' = $counterForFileFound
'L' = $counterFileIsEmpty
'A' = ($counterForFileFound - $counterFileIsEmpty)
'I' = $counterForSumupAllRows - ($counterForTemplates * 36)
'A' = $counterForTemplates
}
$xl.quit()
$tableAllInformation | Export-CSV $CSVFile -NoTypeInformation -Force
$tableAllInformation | Format-Table *
$endResult | Format-Table *
}