对于这么长的问题表示歉意。我正在研究 raku Net::Google::Sheets 模块。
目前,它在成功使用 OAuth2::Client::Google 以及从 Google Sheet 获取和放入 2D 数组方面做得很好。
我想添加一个符合
Google Sheets API的
$sheet.clear
方法。
这是 /lib/Net/Google/Sheets.rakumod 中的相关类:
class Sheet is export {
has $.session;
has $.id;
has $.range;
method url {
"$sheet-base/{$!id}/values/{$!range}"
}
multi method values {
my $request = HTTP::Request.new(
GET => $.url,
Authorization => "Bearer {$.session.token}",
);
$ua.request($request).decoded-content.&from-json<values>;
}
multi method values( $data ) {
my %json-hash := {
range => $!range,
majorDimension => 'ROWS',
values => $data,
};
my $request = HTTP::Request.new(
PUT => "{$.url}?valueInputOption=USER_ENTERED",
Authorization => "Bearer {$.session.token}",
);
$request.add-content( %json-hash.&to-json );
$ua.request( $request );
}
method clear {
my $request = HTTP::Request.new(
POST => ($.url ~ ':clear'),
Authorization => "Bearer {$.session.token}",
Content-length => 0,
# Content-Type => "application/json",
);
# debug lines
say ~$request.header;
say ~$ua.request($request).decoded-content;
# $ua.request($request).decoded-content.&from-json;
}
}
这是调用它的脚本:
#!/usr/bin/env raku
use Net::Google::Sheets;
my $session = Session.new;
my %sheets = $session.sheets;
my $id = %sheets<AWS_EC2_Sizes_test>;
# get values from Sheet1
my $sheet1 = Sheet.new(:$session, :$id, range => 'Sheet1');
my $vals = $sheet1.values;
say $vals; # <=== works fine
# put values into Sheet2
my $sheet2 = Sheet.new(:$session, :$id, range => 'Sheet2');
$sheet2.values: $vals; # <=== works fine
$sheet2.clear; # <=== give error
因此 Oauth 访问读取和写入工作正常,并且工作表 ID 正确。我还通过 Google 参考文档“尝试”框获得了成功的 .clear 操作。我已经尝试了 {range} url 值的许多变体,但这似乎没有效果。
这是我得到的输出和错误......
<p>The requested URL <code>/v4/spreadsheets/{SheetId}/values/Sheet2%3Aclear</code> was not found on this server. <ins>That’s all we know.</ins></p>
如果我打印网址,我会得到这个...
say $.url ~ ':clear'; # insert this debug line before the clear request
https://sheets.googleapis.com/v4/spreadsheets/{SheetId}/values/Sheet2:clear
我怀疑 gRPC 存在一些我没有遵循的微妙之处(也许我需要以另一种方式对“:”冒号字符进行编码?)。
请问您能帮忙吗?
您可以尝试 Raku 的
URI::Encode
模块:
https://raku.land/zef:raku-community-modules/URI::Encode
或者(更直接),您可以尝试
ord
和chr
:
在 Raku REPL 中:
[0] > say "\c[COLON]".ord;
58
[0] > say "\c[COLON]".ord.chr;
:
或者只是
chr
:
[0] > say "58".chr;
: