4

ドキュメント リスト (スプレッドシートの作成用) とスプレッドシート (ワークシートの作成と行の追加用) の API 全体に従いました。ただし、作成後にスプレッドシートにワークシートを追加することはできますが、行を追加しようとすると、次のエラーが発生します:リクエストの実行に失敗しました: https://spreadsheets.google.com/feeds/list/ tj0pIc6qpEB2LtZY9mwfT-A/od6/プライベート/フル

すべての OAuth スコープと必要な資格情報について言及しましたが、この例外を解決できません。Googleスプレッドシートの作成やワークシートの追加など、残りの部分は正常に機能しています。Googleコードをコピーして貼り付けているだけです。

        // setUp the confirguration for OAuth 2.0
        string clientID = "********.apps.googleusercontent.com";
        string clientSecret = "*******************";
        string scope = "https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/";
        string redirectURI = "urn:***:wg:oauth:2.0:oob";

        // setup the OAuth 2.0 object
        OAuth2Parameters parameters = new OAuth2Parameters();   // to hold all the parameters
        parameters.ClientId = clientID; // setup the clientID
        parameters.ClientSecret = clientSecret;  // setup the clientSecret
        parameters.RedirectUri=redirectURI; // setup the redirectURI

        //setup the authurization URL
        parameters.Scope = scope; // set the scope

        string authorizationURL = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
        Console.WriteLine(authorizationURL);
        Console.WriteLine("Please visit the URL above to authorize your OAuth " + "request token.  Once that is complete, type in your access code to "
    + "continue...");
        parameters.AccessCode = Console.ReadLine();

        // get the access token
        OAuthUtil.GetAccessToken(parameters);
        string accessToken = parameters.AccessToken;
        Console.WriteLine("Cosole Access token " + accessToken);

        //make Auth Request to Google
        GOAuth2RequestFactory factory = new GOAuth2RequestFactory(null, "SampleSpreadSheetApp-V1", parameters);
       // DocumentsService service = new DocumentsService("SampleSpreadSheetApp-V1");
        service.RequestFactory = factory;



        //---------------------------------------------------------------------
        GOAuth2RequestFactory requestFactory =
            new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
        SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
        service.RequestFactory = requestFactory;

        SpreadsheetQuery query = new SpreadsheetQuery();
        SpreadsheetFeed feed = service.Query(query);

        if (feed.Entries.Count == 0)
        {
            Console.WriteLine("no spreadsheets present here");
        }

        // TODO: Choose a spreadsheet more intelligently based on your
        // app's needs.
        SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
        Console.WriteLine(spreadsheet.Title.Text);

        // Get the first worksheet of the first spreadsheet.
        // TODO: Choose a worksheet more intelligently based on your
        // app's needs.
        WorksheetFeed wsFeed = spreadsheet.Worksheets;
        WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];

        if (wsFeed.Entries.Count == 0)
        {
            Console.WriteLine("no worksheets present here");
        }

        // Define the URL to request the list feed of the worksheet.
        AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);

        // Fetch the list feed of the worksheet.
        ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
        ListFeed listFeed = service.Query(listQuery);

        // Create a local representation of the new row.
        ListEntry row = new ListEntry();
        row.Elements.Add(new ListEntry.Custom() { LocalName = "firstname", Value = "Joe" });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "lastname", Value = "Smith" });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "age", Value = "26" });
        row.Elements.Add(new ListEntry.Custom() { LocalName = "height", Value = "176" });

        // Send the new row to the API for insertion.
        service.Insert(listFeed, row);
4

4 に答える 4

9

私もこれに苦労しましたが、次のことがわかりました。

各列には、最初の行で指定された名前があります。Google API の例では、最初の列は「firstname」で、既存のワークシートのセル A1 に示されています。

行を追加する場合 (上に貼り付けたコード例のように)、'LocalName' プロパティは正確に一致する必要があります。また、API は列名からスペースを小文字にして削除します (神はその理由を知っていますか??)。そのため、列名を「名前」として定義すると、API はそれを「名前」に小文字にして一致させようとします。したがって、行を追加するときは、コードで列名を小文字でスペースを入れずに定義する必要があります。

安全のために、1 列のワークシートを作成し、最初の行のセルを「name」に設定します。次に、認証を使用してサンプル コードを実行し、次のようにエントリが 1 つだけの行を追加してみます。

ListEntry row = new ListEntry();
row.Elements.Add(new ListEntry.Custom() { LocalName = "name", Value = "John" });

そして、これは機能しません

row.Elements.Add(new ListEntry.Custom() { LocalName = "Name", Value = "John" });
于 2012-12-24T12:48:14.310 に答える
2

最初にワークシートのヘッダー行を作成/確認する必要があります。

        CellQuery cellQuery = new CellQuery(worksheet.CellFeedLink);
        CellFeed cellFeed = service.Query(cellQuery);

        CellEntry cellEntry = new CellEntry(1, 1, "firstname");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 2, "lastname");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 3, "age");
        cellFeed.Insert(cellEntry);
        cellEntry = new CellEntry(1, 4, "height");
        cellFeed.Insert(cellEntry);
于 2014-11-10T04:53:35.230 に答える
1

After banging my head on this for hours, I discovered that the Google API appears to have a bug. If there are blank rows at the bottom of the worksheet, adding a row works fine, and the API returns an appropriate response. If, however, there are no blank rows at the bottom of the worksheet, your data is duly and correctly added to a new row at the bottom of the worksheet, BUT Google nonetheless returns an HTTP 400 error containing the text "Blank rows cannot be written."
You can get around this bug by catching the error, checking that the error is of the "Blank rows" variety and, if so, using a listfeed query to retrieve the row you just added. If that query is successful, then the new row was added correctly and the 400 error can be ignored.
I'd give you my code, but I'm working in PHP, so probably not helpful.

于 2013-04-16T18:42:35.057 に答える
1

ドキュメントで報告されているように、リスト フィードは、データがスプレッドシートにどのように配置されるかについていくつかの仮定を行います。特に、リスト フィードはワークシートの最初の行をヘッダー行として扱います。

https://developers.google.com/google-apps/spreadsheets/#working_with_list-based_feeds

あなたのコードでは、firstname、lastname、age、height の 4 つの値を持つ行を追加しようとしています。ワークシートの最初の行でこれらのヘッダーが定義されていない場合、リクエストは無効になり、コードは例外をスローします。

コードがスプレッドシートの構造と一致していることを確認するか、ヘッダー行の内容を動的に決定する必要がある場合はセル フィードを使用してください。

于 2012-10-24T18:37:20.850 に答える