SharePoint Online: เพิ่มคอลัมน์จากการคำนวณลงในรายการโดยใช้ PowerShell
ความต้องการ: เพิ่มคอลัมน์จากการคำนวณลงในรายการ SharePoint Online
จะเพิ่มคอลัมน์จากการคำนวณลงในรายการ SharePoint Online ได้อย่างไร
คอลัมน์จากการคำนวณใน SharePoint Online ใช้เพื่อจัดการคอลัมน์อื่นๆ ในรายการ แทนที่จะป้อนโดยผู้ใช้ ค่าของฟิลด์เหล่านี้จะถูกคำนวณโดยอัตโนมัติตามสูตร เขตข้อมูลจากการคำนวณจะไม่ปรากฏบนแบบฟอร์มใหม่หรือแก้ไขแบบฟอร์ม แต่จะมองเห็นได้บนมุมมองฟอร์มและมุมมอง SharePoint เมื่อต้องการเพิ่มคอลัมน์จากการคำนวณลงในรายการ SharePoint Online ให้ทำดังต่อไปนี้:
- เรียกดูไซต์ SharePoint Online ของคุณและนำทางไปยังรายการเป้าหมายที่คุณต้องการสร้างคอลัมน์จากการคำนวณ
- ใต้แท็บรายการคลิกที่ปุ่ม "สร้างคอลัมน์" ใน Ribbon
ระบุชื่อให้กับคอลัมน์ใหม่ของคุณ ระบุประเภทเป็น "คำนวณ (คำนวณตามคอลัมน์อื่น)"
- ป้อนสูตรสำหรับคอลัมน์จากการคำนวณของคุณในช่อง "สูตร" และระบุประเภทการส่งคืนของข้อมูล
- เลื่อนลง ตั้งค่าพารามิเตอร์ เช่น ตำแหน่งทศนิยมและรูปแบบข้อมูล แล้วคลิก "ตกลง" เพื่อสร้างฟิลด์จากการคำนวณในรายการ SharePoint Online
PowerShell เพื่อสร้างคอลัมน์จากการคำนวณเพื่อแสดงรายการใน SharePoint Online
คุณสามารถใช้ SharePoint Online PowerShell เพื่อเพิ่มคอลัมน์จากการคำนวณลงในรายการ:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
#Custom function to add column to list
Function Add-CalculatedColumnToList()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $ListName,
[Parameter(Mandatory=$true)] [string] $Name,
[Parameter(Mandatory=$true)] [string] $DisplayName,
[Parameter(Mandatory=$false)] [string] $Description=[string]::Empty,
[Parameter(Mandatory=$true)] [string] $FieldsReferenced,
[Parameter(Mandatory=$true)] [string] $Formula,
[Parameter(Mandatory=$true)] [string] $ResultType
)
#Generate new GUID for Field ID
$FieldID = New-Guid
Try {
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Check if the column exists in list already
$Fields = $List.Fields
$Ctx.Load($Fields)
$Ctx.executeQuery()
$NewField = $Fields | where { ($_.Internalname -eq $Name) -or ($_.Title -eq $DisplayName) }
if($NewField -ne $NULL)
{
Write-host "Column $Name already exists in the List!" -f Yellow
}
else
{
#Frame FieldRef Field
$FieldRefXML=[string]::Empty
$FieldRefs = $FieldsReferenced.Split(",")
foreach ($Ref in $FieldRefs)
{
$FieldRefXML = $FieldRefXML + "<FieldRef Name='$Ref' />"
}
#Create Column in the list
$FieldSchema = "<Field Type='Calculated' ID='{$FieldID}' DisplayName='$DisplayName' Name='$Name' Description='$Description' ResultType='$ResultType' ReadOnly='TRUE'><Formula>$Formula</Formula><FieldRefs>$FieldRefXML</FieldRefs></Field>"
$FieldSchema
$NewField = $List.Fields.AddFieldAsXml($FieldSchema,$True,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldInternalNameHint)
$Ctx.ExecuteQuery()
Write-host "New Column Added to the List Successfully!" -ForegroundColor Green
}
}
Catch {
write-host -f Red "Error Adding Column to List!" $_.Exception.Message
}
}
#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$Name="TotalInvestmentBuffer"
$DisplayName="Total Investment Buffer"
$Description="5 Percentage in Total Investment"
$Formula = "=([Total Investment]*5)/100"
$ResultType="Currency"
$FieldsReferenced="[Total Investment]" #Fields Names participating in Formula. Should be Comma (,) Separated
#Call the function to add column to list
Add-CalculatedColumnToList -SiteURL $SiteURL -ListName $ListName -Name $Name -DisplayName $DisplayName -Description $Description -Formula $Formula -ResultType $ResultType -FieldsReferenced $FieldsReferenced
PnP PowerShell เพื่อเพิ่มคอลัมน์จากการคำนวณลงในรายการใน SharePoint Online
ต่อไปนี้เป็นวิธีเพิ่มคอลัมน์จากการคำนวณลงในรายการโดยใช้ PnP PowerShell:
#Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Add a calculated column to list
Add-PnPField -List $ListName -Type Calculated -InternalName "Buffer" -DisplayName "Buffer" -Formula "=[Budget]*0.10"
