やりたいこと
PHPよりGoogle APIを使用してスプレッドシートのセルを指定してコメントを追加したいです。
コメントはスプレッドシートに作成されるが、セルとのリンクがうまくいかないです。
手詰まりでヒントでもありましたら、よろしくお願いします。
参考
試したこと
配列で指定
ソース
PHP
1<?php 2require_once('client.php'); 3 4$fileId = '<スプレッドシートID>'; 5 6$client = getClient(); 7 8$service = new Google_Service_Drive($client); 9$response=$service->revisions->get($fileId, "head"); 10$revisionId = $response->id; 11$comment_data = new Google_Service_Drive_Comment($client); 12$comment_data->setContent("APIから作成({$revisionId})"); 13$comment_data->setAnchor([ 14 'r' => (int)$revisionId, 15 'a' => [ 16 [ 17 'matrix' => [ 18 'c' => 2, 19 'r' => 1, 20 'w' => 1, 21 'h' => 1, 22 'mw' => 1, 23 'mh' => 1, 24 ], 25 'page' => [ 26 'p' => 0, 27 'mp' => 3, 28 ], 29 ], 30 ], 31]); 32 33$response = $service->comments->create($fileId, $comment_data,['fields'=>'id,content,anchor']); 34print "anchor[{$response->anchor}]\n"; 35print "commentId[{$response->id}]\n";
結果
bash
1$ php createComment.php 2anchor[] 3commentId[AAAABWAI0z8]
anchor
に何も返ってこない。
スプレッドシートを見るとコメントは追加されているが、セルにリンクされていない。
setAnchor()に不正な値にしてみる
ソース
PHP
1 : : 2$comment_data->setAnchor([ 3 'r' => (int)$revisionId, 4 'a' => [ 5 [ 6 'matrix-' => [ 7 'c' => 2, 8 : :
matrix
を matrix-
に変えてみる。
結果
bash
1$ php createComment.php 2anchor[] 3commentId[AAAABWAI01I]
エラーも何もでない。 anchor
にはやはり返ってこない。
解析すらしていない?
配列から文字列にしてみる
ソース
PHP
1 : : 2$comment_data->setAnchor("{'r':'$revisionId','a':[{'matrix':{'c':2,'r':1,'w':1,'h':1,'mw':1,'mh':1},'page':{'p':0,'mp':3}}]}"); 3 : :
結果
bash
1$ php createComment.php 2anchor[{'r':'137','a':[{'matrix':{'c':2,'r':1,'w':1,'h':1,'mw':1,'mh':1},'page':{'p':0,'mp':3}}]}] 3commentId[AAAABWAI02I]
anchor
返ってきた。
しかし、セルとのリンクはなし(コメントのみ)
Comments.list
で確認すると
- APIコメント(セルとリンクなし)
[anchor] => {'r':'137','a':[{'matrix':{'c':2,'r':1,'w':1,'h':1,'mw':1,'mh':1},'page':{'p':0,'mp':3}}]}
- 手入力コメント(セルとリンク)
[anchor] => {"type":"workbook-range","uid":0,"range":"216466109"}
手入力のほうは説明と全然違う値が入っている type
とか range
とか。
試しに文字列の方も違った値を入れてみる
ソース
PHP
1 : : 2$comment_data->setAnchor("{'r':'$revisionId','a':[{'matrix-':{'c':2,'r':1,'w':1,'h':1,'mw':1,'mh':1},'page':{'p':0,'mp':3}}]}"); 3 : :
同じようにmatrix-
にしてみる。
結果
bash
1$ php createComment.php 2PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ 3 "error": { 4 "errors": [ 5 { 6 "domain": "global", 7 "reason": "invalid", 8 "message": "The anchor property matrix- is unknown.", 9 "locationType": "other", 10 "location": "comment.anchor" 11 } 12 ], 13 "code": 400, 14 "message": "The anchor property matrix- is unknown." 15 } 16} 17' 18 : :
エラーがでたので解析はしているよう。
あなたの回答
tips
プレビュー