【问题标题】:Store data in google sheets from Azure functions将数据存储在 Azure 函数的 google 表格中
【发布时间】:2017-01-07 08:52:50
【问题描述】:

我正在尝试将数据从 azure 函数插入到谷歌表格中。 在 azure 函数的集成选项卡中,我选择了新的输出作为外部表并选择了 googleSheets 并创建了一个连接字符串。但我没有看到任何文件显示我们如何从/插入到 excel 表中的数据。有快速入门的示例吗?

【问题讨论】:

    标签: azure google-sheets azure-functions


    【解决方案1】:

    下面是使用 Azure 函数绑定到表格连接器的简单示例。我已经验证它适用于 SQL Server、Google Sheets 和 Salesforce。理论上,只要它实现了连接器数据协议 (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 数据库表 谷歌表格电子表格工作表 Excel Excel文件表

    对于表绑定,entityId 必须为空。 绑定

    表格绑定(如上例所示)可能是最有用的。以下是受支持的 C# 绑定的完整列表及其要求:

    表客户端 参数类型必须为 ITableClient。 dataSetName、tableName 和 entityId 必须为空。

    表 参数类型必须是 ITable(TEntity 是 POCO 类型)、ITable、IAsyncCollector 或 IAsyncCollector。必须提供 dataSetName 和 tableName。 entityId 必须为空。

    实体 参数类型必须是 TEntity(POCO 类型)或 JObject。必须提供 dataSetName、tableName 和 entityId。 接口

    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 服务器

    创建和填充联系人表的脚本如下。 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 文档中创建一个包含名为 Contact 的工作表的电子表格。连接器不能使用电子表格显示名称。内部名称(粗体)需要用作dataSetName,例如:https://docs.google.com/spreadsheets/d/1UIz545JF_cx6Chm_5HpSPVOenU4DZh4bDxbFgJOSMz0
    将列名 Id、LastName、FirstName 添加到第一行,然后在后续行中填充数据。

    销售人员

    dataSetName 是“默认”。

    【讨论】:

    • 我创建了一个手动触发器,添加了外部表作为输入。选择了 Google 表格并允许访问我的 gmail。它创建了一个自动存储在密钥设置中的连接字符串。然后我留下了下面的代码,并得到了同一示例中未找到的错误资源。知道我做错了什么。 { "type": "apiHubTable", "name": "table", "dataSetName": "https://docs.google.com/spreadsheets/d/1QNi_-CQTPuB5IsFSBXu0RHQBk9YrBtgoTNWz5NjfMic", "connection": "googlesheet_GOOGLESHEET", "direction": "in", "tableName": "Contact" }
    • 遇到与@Robert 相同的错误。也使用 Google 电子表格。我认为您的示例需要包含对所用电子表格的访问权限。
    • 工具很差,我无法想象在生产中支持/调试它。
    • 这是来自docs.microsoft.com/en-us/azure/azure-functions/… 的直接复制/粘贴,您应该链接到它而不是在此处重复示例。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多