我已经将Google设置为一个简单的后端,使用了针对php网站的api。它是一个外语学习资源,所以人们可以添加/编辑句子。
在生成每种语言的页面的template.php的顶部,我有这个脚本,然后在$sheetsValues上循环生成站点上的表。
<?php
require __DIR__ . '/vendor/autoload.php';
// connect to API
$client = new \Google_Client();
$client->setApplicationName('12Sentences');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/credentials.json');
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'MY_SPREADSHEET_ID';
$range = "$language!B3:E15";
$response = $service->spreadsheets_values->get($spreadsheetId, $range );
$sheetsValues = $response->getValues();
?>
虽然它没有从工作表中获取颜色和格式信息。我想让google页面的文本颜色显示在网站上(比如红色的'La Pomme') -因为它可以用来表示句子的元素(主语、动词、宾语顺序等等)。使用可以吗?
谢谢,
托马斯
更新后的代码:在函数getRes()中使用Tanaike的解决方案从中获取颜色数据。然后调用getFormattedHtml()以获取HTML和CSS中的有色文本。虽然不雅致,但对我的使用效果很好。
<?php
function getFormattedHtml($row, $column, $res) {
$formattedHtml = "";
// get plain text
$plain_text = $res[$row][$column]['formattedValue'];
// get textFormatRuns
$textFormatRuns = $res[$row][$column]['textFormatRuns'];
// loop over the textFormatRuns
$len = count($textFormatRuns);
for ($i=0; $i < $len; $i++) {
$currentRunStart = $textFormatRuns[$i]['startIndex'];
$currentRunEnd = $textFormatRuns[$i + 1]['startIndex'];
$substring = "";
if ($i == $len - 1) {
$substring = substr($plain_text, $currentRunStart);
} else {
$substring = substr($plain_text, $currentRunStart, $currentRunEnd - $currentRunStart);
}
$span = "";
if (isset($textFormatRuns[$i]['format']['foregroundColor'])) {
$red = $textFormatRuns[$i]['format']['foregroundColor']['red'] * 255;
$green = $textFormatRuns[$i]['format']['foregroundColor']['green'] * 255;
$blue = $textFormatRuns[$i]['format']['foregroundColor']['blue'] * 255;
$span = "<span style=\"color:rgb($red, $green, $blue)\">$substring</span>";
} else {
$span = "<span>$substring</span>";
}
$formattedHtml .= $span;
}
return($formattedHtml);
}
function getRes() {
require __DIR__ . '/vendor/autoload.php';
// connect to API
$client = new \Google_Client();
$client->setApplicationName('12Sentences');
$client->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$client->setAccessType('offline');
$client->setAuthConfig(__DIR__ . '/credentials.json');
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'MY_SPREADSHEET_ID';
$range = "$language!B3:E15";
// This script uses the method of "spreadsheets.get".
$sheets = $service->spreadsheets->get($spreadsheetId, ["ranges" => [$range], "fields" => "sheets"])->getSheets();
// Following script is a sample script for retrieving "textFormat" and "textFormatRuns".
$data = $sheets[0] -> getData();
$startRow = $data[0] -> getStartRow();
$startColumn = $data[0] -> getStartColumn();
$rowData = $data[0] -> getRowData();
$res = array();
foreach ($rowData as $i => $row) {
$temp = array();
foreach ($row -> getValues() as $j => $value) {
$tempObj = [
"row" => $i + 1 + $startRow,
"column" => $j + 1 + $startColumn
];
if (isset($value['formattedValue'])) {
$tempObj['formattedValue'] = $value -> getFormattedValue();
} else {
$tempObj['formattedValue'] = "";
}
$userEnteredFormat = $value -> getUserEnteredFormat();
if (isset($userEnteredFormat['textFormat'])) {
$tempObj['textFormat'] = $userEnteredFormat -> getTextFormat();
} else {
$tempObj['textFormat'] = null;
}
if (isset($value['textFormatRuns'])) {
$tempObj['textFormatRuns'] = $value -> getTextFormatRuns();
} else {
$tempObj['textFormatRuns'] = null;
}
array_push($temp, $tempObj);
}
array_push($res, $temp);
}
return($res);
}
?>
发布于 2021-05-16 17:42:24
我相信你的目标如下。
修改要点:
当以上要点反映到您的脚本中时,如下所示。
修改脚本:
$service = new Google_Service_Sheets($client);
$spreadsheetId = 'MY_SPREADSHEET_ID';
$range = "$language!B3:E15";
// This script uses the method of "spreadsheets.get".
$sheets = $service->spreadsheets->get($spreadSheetId, ["ranges" => [$range], "fields" => "sheets"])->getSheets();
// Following script is a sample script for retrieving "textFormat" and "textFormatRuns".
$data = $sheets[0] -> getData();
$startRow = $data[0] -> getStartRow();
$startColumn = $data[0] -> getStartColumn();
$rowData = $data[0] -> getRowData();
$res = array();
foreach ($rowData as $i => $row) {
$temp = array();
foreach ($row -> getValues() as $j => $value) {
$tempObj = [
"row" => $i + 1 + $startRow,
"column" => $j + 1 + $startColumn
];
if (isset($value['formattedValue'])) {
$tempObj['formattedValue'] = $value -> getFormattedValue();
} else {
$tempObj['formattedValue'] = "";
}
$userEnteredFormat = $value -> getUserEnteredFormat();
if (isset($userEnteredFormat['textFormat'])) {
$tempObj['textFormat'] = $userEnteredFormat -> getTextFormat();
} else {
$tempObj['textFormat'] = null;
}
if (isset($value['textFormatRuns'])) {
$tempObj['textFormatRuns'] = $value -> getTextFormatRuns();
} else {
$tempObj['textFormatRuns'] = null;
}
array_push($temp, $tempObj);
}
array_push($res, $temp);
}
print($res);
结果:
当上面的脚本用于以下情况时,
得到了如下结果。
[
[
{
"row":1,
"column":1,
"formattedValue":"sample1 sample2 sample3 sample4 sample5",
"textFormat":{"bold":true},
"textFormatRuns":[
{"startIndex":8,"format":{"foregroundColor":{"red":1},"foregroundColorStyle":{"rgbColor":{"red":1}}}},
{"startIndex":15,},
{"startIndex":16,"format":{"foregroundColor":{"green":1},"foregroundColorStyle":{"rgbColor":{"green":1,}}}},
{"startIndex":23,},
{"startIndex":24,"format":{"foregroundColor":{"blue":1},"foregroundColorStyle":{"rgbColor":{"blue":1,}}}},
{"startIndex":31,}]
}
]
]
sample2
为红色。rgbColor
的颜色,您可以在正式文档中看到详细信息。参考注意:
参考资料:
https://stackoverflow.com/questions/67557680
复制相似问题