2

I'm trying to write a Google Sheet from an C# Azure function using Google Sheets Api. The problem may be silly but I don't seem to be able to find a way to add reference to it.

Following documentation, I've set my function as:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}

I get errors on the 'using' lines for Google.Apis. What I get is:

...error CS0246: The type or namespace name 'Google' could not be found (are you missing a using directive or an assembly reference?)

I understand I have to add a reference like #r "Google" at the very start, but I can't actually find which one exactly; I didn't even find if this is something possible to do.

How should I set it up to work with Google Sheet API?

Rodrigo.A92
  • 691
  • 8
  • 16

2 Answers2

1

It seems the google sheet package hasn't been imported to your azure function.

If you use azure function v2, you need to upload a function.proj file to the function's folder to add the reference of your nuget package. And if you use azure function v1, please upload a project.json instead.

Here is an example of function.proj in azure function v2(since you want to use Google.Apis.Sheets.v4, so I post the example of Google.Apis.Sheets.v4 for your reference, if you want to add some other package you can add ""):

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Google.Apis.Sheets.v4" Version="1.41.1.1713" />
    </ItemGroup>
</Project>

After create the function.proj, please upload it to the function's folder by following the steps below:

  1. Go into your function on Azure portal and click "View files" on the right of your page. enter image description here

  2. Click "Upload" to upload your function.proj file, if you don't want to upload it, you can also click "Add" to add the function.proj file and edit it on portal. enter image description here

After that, please notice the Logs at the bottom of you page to see if the packages restored successfully. And then you can use google sheet api.

For further information of how to use nuget package in azure function, please refer to this tutorial: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#using-nuget-packages

Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • Thank you very much! I have tried that the same night but I wasn't able to make it work as .proj file for some reason. What it worked for me, was doing the same as you but on .json file (project.json). See my posted answer. It worked but I don't know if it's ideal. Could you let me know why do we need to add a file to install packages instead of adding a reference (#r) like the other packages we could normally use? – Rodrigo.A92 Sep 23 '19 at 12:57
  • Hi @Rodrigo.A92, I mentioned it in my answer that the function.proj matches azure function V2 and project.json matches azure function V1. So I guess you should use azure function V1. For your question about why do we need to add a file to install package, because this file provides the reference of the packages and it could be easy to import the package which you want. – Hury Shen Sep 24 '19 at 02:26
  • @Rodrigo.A92 If you want to use (#r), you need to find the .dll file of your package before that. You can also refer to [this](https://stackoverflow.com/questions/57966783/how-can-i-add-ssh-net-nuget-packages-into-my-azure-functions-v2-0/57966824#57966824) post which I wrote a few days ago to know how to use (#r) to import your package. If the solution helps your issue, could you please mark my answer as "accepted", thanks~ – Hury Shen Sep 24 '19 at 02:28
0

I've tried what @Hury nicely explained and detailed, and as far as I've researched (and has been a lot) it worked for a lot of people. But unfortunatelly for some reason I couldn't make the function.proj file work. I must be missing something.

What I was able to make it work is creating the following project.json file:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Google.Apis.Sheets.v4": "1.41.1.1713",
        "Newtonsoft.Json": "10.0.0.0"
      }
    }
   }
}

The process of creating it is the same as Hury explained, or you can write it directly on Portal. That version of Google API is at time of writing. I've added Newtonsoft because it seems to be needed by Google API, and its version seems to be required specifically.

Rodrigo.A92
  • 691
  • 8
  • 16