Related topics
×
Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

使用 PY 函數 從 Microsoft 365 Excel 方格存取 Excel 中的 Python。  

重要: 在儲存格中輸入 =PY 並 從 [自動完成] 功能表選取 PY 函數之後,儲存格編輯體驗會轉換成 Python 編輯器。 若要深入瞭解,請參閱 Excel 中的 Python 簡介Excel 中的 Python 快速入門。 

語法 

PY 函數會在安全的 Microsoft Cloud 執行時間上執行 Python 程式碼。 結果會以 Python 物件或 Excel 值的形式傳回。 

=PY (python_code,return_type)  

參數

描述

python_code

(必要)

要在安全的 Microsoft Cloud 執行時間上執行的 Python 程式碼。 必須是靜態文字。

return_type

(必要)

指定想要的退貨類型。 必須是靜態數位 0 或 1。 

  • 0 表示 Excel 值。

  • 1 表示 Python 物件。

Excel 參照 

Python 程式碼可以使用自訂的 Python 函數來參照 Excel 中的值 xl().  

xl()函數支援參照下列 Excel 物件。

  • 範圍

  • 名字

  • 資料表

  • Power Query連線

xl() 函數支援選用的頁首引數。 標題引數會指定第一列是否有標題。 例如, xl("A1:Z10", headers=True)表示 儲存格 A1:Z1 是標題。 

註解 

  • PY函數無法搭配任何其他 Excel 函數使用。 如果與其他函數搭配使用,則無法輸入公式。

  • FORMULATEXT可看到PY函數,並使用增益集撰寫和讀取。

  • python_codereturn_type引數都必須是靜態的。

範例 

範例 1 使用 Python 加號運算子從 3 個文字片段產生「Hello World」。 

=PY('Hello' + ' ' + 'World',0) 

 ——————————————————————————— 

範例 2 使用 xl() 函數從Table1 取得資料,並將結果傳回為 Python 物件。 

=PY("xl(""Table1[#All]"", headers=True)",1) 

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×