我正在尝试从 azure 函数将数据插入到 google 工作表中。 在 azure 函数的集成选项卡中,我选择了新的输出作为外部表,并选择了 googleSheets,并创建了一个连接字符串。但我没有看到任何文档显示我们如何从 Excel 工作表中读取数据/将数据插入到 Excel 工作表中。有快速启动的快速示例吗?
最佳答案
下面是使用 Azure 函数绑定(bind)到表格连接器的简单示例。我已经验证它可以与 SQL Server、Google Sheets 和 Salesforce 配合使用。理论上,只要它实现连接器数据协议(protocol)(CDP),它就应该与任何表格连接器一起使用。 开发
#r "Microsoft.Azure.ApiHub.Sdk"
#r "Newtonsoft.Json"
using System;
using Microsoft.Azure.ApiHub;
public class Contact
{
public string Id { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
}
public static async Task Run(string input, ITable<Contact> table, TraceWriter log)
{
ContinuationToken continuationToken = null;
do
{
var contactsSegment = await table.ListEntitiesAsync(
continuationToken: continuationToken);
foreach (var contact in contactsSegment.Items)
{
log.Info(string.Format("{0} {1}", contact.FirstName, contact.LastName));
}
continuationToken = contactsSegment.ContinuationToken;
}
while (continuationToken != null);
}
为了简单起见,该示例使用手动触发器。不使用触发器的输入值。 该示例假设连接器提供一个包含 Id、LastName 和 FirstName 列的联系人表。该代码列出了表中的联系人实体并记录名字和姓氏。 整合
{
"bindings": [
{
"type": "manualTrigger",
"direction": "in",
"name": "input"
},
{
"type": "apiHubTable",
"direction": "in",
"name": "table",
"connection": "ConnectionAppSettingsKey",
"dataSetName": "default",
"tableName": "Contact",
"entityId": "",
}
],
"disabled": false
}
ConnectionAppSettingsKey 标识存储连接字符串的应用设置。
表格连接器提供数据集,每个数据集都包含表格。默认数据集的名称是“default”。这些概念由 dataSetName 和 tableName 标识,并且特定于每个连接器:
连接器数据集表 SharePoint 网站 SharePoint 列表 SQL 数据库表 Google Sheet 电子表格 工作表 Excel Excel 文件工作表
对于表绑定(bind),entityId 必须为空。 绑定(bind)
表绑定(bind)(如上所示)可能是最有用的。以下是受支持的 C# 绑定(bind)的完整列表及其要求:
表格客户端 参数类型必须为ITableClient。 dataSetName、tableName 和entityId 必须为空。
表 参数类型必须是 ITable(TEntity 是 POCO 类型)、ITable、IAsyncCollector 或 IAsyncCollector。必须提供数据集名称和表名称。 EntityId 必须为空。
实体 参数类型必须是TEntity(POCO类型)或JObject。必须提供数据集名称、表名称和实体 ID。 接口(interface)
public interface ITableClient
{
/// <summary>
/// Gets a reference to a data set.
/// </summary>
/// <param name="dataSetName">The name of the data set.</param>
/// <returns>The data set reference.</returns>
IDataSet GetDataSetReference(string dataSetName = null);
/// <summary>
/// Queries the table client for data sets.
/// </summary>
/// <param name="query">The query to be executed.</param>
/// <param name="continuationToken">A continuation token from the server
/// when the operation returns a partial result.</param>
/// <param name="cancellationToken">A cancellation token that can be used
/// by other objects or threads to receive notice of cancellation.</param>
/// <returns>The retrieved data sets. In case of partial result the
/// object returned will have a continuation token.</returns>
Task<SegmentedResult<IDataSet>> ListDataSetsAsync(
Query query = null,
ContinuationToken continuationToken = null,
CancellationToken cancellationToken = default(CancellationToken));
}
public interface IDataSet
{
/// <summary>
/// Gets the data set name.
/// </summary>
string DataSetName { get; }
/// <summary>
/// Gets the data set display name.
/// </summary>
string DisplayName { get; }
/// <summary>
/// Gets a reference to a table.
/// </summary>
/// <typeparam name="TEntity">The type of entities in the table.</typeparam>
/// <param name="tableName">The name of the table.</param>
/// <returns>The table reference.</returns>
ITable<TEntity> GetTableReference<TEntity>(string tableName)
where TEntity : class;
/// <summary>
/// Queries the data set for tables.
/// </summary>
/// <param name="query">The query to be executed.</param>
/// <param name="continuationToken">A continuation token from the server
/// when the operation returns a partial result.</param>
/// <param name="cancellationToken">A cancellation token that can be used
/// by other objects or threads to receive notice of cancellation.</param>
/// <returns>The retrieved tables. In case of partial result the
/// object returned will have a continuation token.</returns>
Task<SegmentedResult<ITable<JObject>>> ListTablesAsync(
Query query = null,
ContinuationToken continuationToken = null,
CancellationToken cancellationToken = default(CancellationToken));
}
public interface ITable<TEntity>
where TEntity : class
{
/// <summary>
/// Gets the data set name.
/// </summary>
string DataSetName { get; }
/// <summary>
/// Gets the table name.
/// </summary>
string TableName { get; }
/// <summary>
/// Gets the table display name.
/// </summary>
string DisplayName { get; }
/// <summary>
/// Retrieves table metadata.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used
/// by other objects or threads to receive notice of cancellation.</param>
/// <returns>The table metadata.</returns>
Task<TableMetadata> GetMetadataAsync(
CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Retrieves the entity with the specified identifier.
/// </summary>
/// <param name="entityId">The entity identifier.</param>
/// <param name="cancellationToken">A cancellation token that can be used
/// by other objects or threads to receive notice of cancellation.</param>
/// <returns>The entity or null if not found.</returns>
Task<TEntity> GetEntityAsync(
string entityId,
CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Queries the table for entities.
/// </summary>
/// <param name="query">The query to be executed.</param>
/// <param name="continuationToken">A continuation token from the server
/// when the operation returns a partial result.</param>
/// <param name="cancellationToken">A cancellation token that can be used
/// by other objects or threads to receive notice of cancellation.</param>
/// <returns>The retrieved entities. In case of partial result the
/// object returned will have a continuation token.</returns>
Task<SegmentedResult<TEntity>> ListEntitiesAsync(
Query query = null,
ContinuationToken continuationToken = null,
CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Adds a new entity to the table.
/// </summary>
/// <param name="entity">The entity to be created.</param>
/// <param name="cancellationToken">A cancellation token that can be used
/// by other objects or threads to receive notice of cancellation.</param>
/// <returns></returns>
Task CreateEntityAsync(
TEntity entity,
CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Updates an existing entity.
/// </summary>
/// <param name="entityId">The entity identifier.</param>
/// <param name="entity">The updated entity.</param>
/// <param name="cancellationToken">A cancellation token that can be used
/// by other objects or threads to receive notice of cancellation.</param>
/// <returns></returns>
Task UpdateEntityAsync(
string entityId,
TEntity entity,
CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Deletes an existing entity.
/// </summary>
/// <param name="entityId">The entity identifier.</param>
/// <param name="cancellationToken">A cancellation token that can be used
/// by other objects or threads to receive notice of cancellation.</param>
/// <returns></returns>
Task DeleteEntityAsync(
string entityId,
CancellationToken cancellationToken = default(CancellationToken));
}
注释
以下是尝试该示例的一些说明:
SQL Server
用于创建和填充联系人表的脚本如下。 dataSetName 是“默认”。
CREATE TABLE Contact
(
Id int NOT NULL,
LastName varchar(20) NOT NULL,
FirstName varchar(20) NOT NULL,
CONSTRAINT PK_Contact_Id PRIMARY KEY (Id)
)
GO
INSERT INTO Contact(Id, LastName, FirstName)
VALUES (1, 'Bitt', 'Prad')
GO
INSERT INTO Contact(Id, LastName, FirstName)
VALUES (2, 'Glooney', 'Ceorge')
GO
Google 表格
在 Google 文档中创建一个电子表格,其中包含名为“联系人”的工作表。连接器无法使用电子表格显示名称。 dataSetName 需要使用内部名称(粗体),例如:https://docs.google.com/spreadsheets/d/ 1UIz545JF_cx6Chm_5HpSPVOenU4DZh4bDxbFgJOSMz0
将列名 Id、LastName、FirstName 添加到第一行,并在后续行中填充数据。
销售人员
dataSetName 是“默认”。
关于azure - 从 Azure 函数将数据存储在 Google Sheets 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519631/