我安装了以下内容:
https://github.com/google/google-api-php-client-services
正在使用
https://github.com/google/google-api-php-client-services/tree/master/src/Google/Service/Sheets
我想做的是:
$cellFormat = new Google_Service_Sheets_CellFormat();
$color = $cellFormat->getBackgroundColor();
但我无法弄清楚如何分配电子表格或选择范围。然后当然返回响应。
非常感谢任何帮助!
答案 0 :(得分:0)
我无法找到google电子表格v4语法的任何文档,所以我使用谷歌APP脚本解决了它。
首先我得到了表格:
var Sheet = SpreadsheetApp.openById("XXXXXXSPREADSHEETXXXXXID").getSheets();
var tracker = "NEWXXXXXXSPREADSHEETXXXXXID";
var date = new Date();
var M = date.getMonth();
var Y = date.getFullYear();
var Sheettemp = SpreadsheetApp.openById(tracker);
var SheetTr = Sheettemp.getSheets()[M];
var templateSheet = Sheettemp.getSheetByName('Empty');
Sheettemp.insertSheet(M, {template: templateSheet});
Sheettemp.deleteSheet(SheetTr);
SheetTr = Sheettemp.getSheets()[M];
SheetTr.setName(months[M]);
var ctrak = 1;
var ctrak2 = 1;
M = M + 1;
然后我循环查看日期并将其记录到数组中:
for (var S in Sheet) {
var A = Sheet[S].getName();
var data = Sheet[S].getRange("D:D").getValues();
var start = M + '/' + 1 + '/' + Y;
var sdate = new Date(start);
var step = [];
var lastdate = "";
for (var i in data) {
var dateval = data[i][0];
var dchk = new Date(dateval);
if (!isNaN(dchk.valueOf())) {
if (lastdate === "") {
if (dchk.getMonth() === sdate.getMonth()) {
lastdate = dchk;
start = i;
var MM = (dchk.getMonth() + 1);
var DD = dchk.getDate();
var YY = dchk.getFullYear();
var arrdate = MM + '/' + DD + '/' + YY;
var j = parseInt(i) + 1;
step.push([j, arrdate]);
}
} else {
if (dchk.toString() !== lastdate.toString()) {
lastdate = dchk;
var stplng = step.length - 1;
if (stplng >= 0) {
step[step.length - 1][2] = (i);
}
var MM = (dchk.getMonth() + 1);
var DD = dchk.getDate();
var YY = dchk.getFullYear();
var arrdate = MM + '/' + DD + '/' + YY;
var j = parseInt(i) + 1;
step.push([j, arrdate]);
start = i;
}
}
}
}
然后,对于每个日期,我获得行的值并将其与我需要的匹配。 (我在这里搜索了一个字符串" getValues",而不是getBackgroundColor,如我的问题中所述):
var count = [];
var count2 = [];
for (var r in step) {
var newrange = "F" + step[r][0] + ":F" + ((step[r][2] !== undefined) ? step[r][2] : "");
var data2 = Sheet[S].getRange(newrange.toString()).getValues();
for (var i in data2) {
if (data2[i][0].indexOf("AN EXAMPLE VALUE") !== -1) {
if (count[r] === undefined) {
count[r] = [];
count[r].push(step[r][1]);
count[r].push('1');
count[r].push(i);
} else {
count[r].push(i);
}
}
if (data2[i][0].indexOf("AN EXAMPLE VALUE 2") !== -1 || data2[i][0].indexOf("AN EXAMPLE VALUE 3") !== -1) {
if (count2[r] === undefined) {
count2[r] = [];
count2[r].push(step[r][1]);
count2[r].push('2');
count2[r].push(i);
} else {
count2[r].push(i);
}
}
}
}
最后我设置了一个包含这些值的新电子表格(我想这可能是在同一个原始电子表格上,或者是ajax调用,不确定ajax是否通过脚本控制台工作)
for (var c in count) {
var B = count[c][0];
var C = count[c][1];
count[c].splice(0, 2);
var D = count[c].length;
if (D > 0) {
var srange = "A" + ctrak + ":D" + ctrak;
srange = srange.toString();
var valuestrak = [[C, A, D, B]];
SheetTr.getRange(srange).setValues(valuestrak);
ctrak++;
}
}
for (var c in count2) {
var B = count2[c][0];
var C = count2[c][1];
count2[c].splice(0, 2);
var D = count2[c].length;
if (D > 0) {
var srange = "E" + ctrak2 + ":H" + ctrak2;
srange = srange.toString();
var valuestrak = [[C, A, D, B]];
SheetTr.getRange(srange).setValues(valuestrak);
ctrak2++;
}
}
最后,我使用我设置的范围从服务器提取信息:
$KEY_FILE_LOCATION = 'client_secret.json';
$client = new Google_Client();
$client->setApplicationName("xxxx");
$client->setAuthConfig($KEY_FILE_LOCATION);
$user_to_impersonate = 'xxx@xx.com';
$client->setSubject($user_to_impersonate);
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/spreadsheets']);
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'NEWXXXXXXSPREADSHEETXXXXXID';
$df = date('F');
echo $df . PHP_EOL;
$range = $df . '!A:D';
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$vals = $response->getValues();
$values = [];
if (!empty($vals)) {
if (is_array($vals)) {
$values = $vals;
} else {
array_push($values, $vals);
}
}