首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何从Google中获取文本颜色格式?

如何从Google中获取文本颜色格式?
EN

Stack Overflow用户
提问于 2021-05-16 06:16:49
回答 1查看 651关注 0票数 1

我已经将Google设置为一个简单的后端,使用了针对php网站的api。它是一个外语学习资源,所以人们可以添加/编辑句子。

google工作表的截图

从工作表生成的站点的屏幕截图

在生成每种语言的页面的template.php的顶部,我有这个脚本,然后在$sheetsValues上循环生成站点上的表。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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中的有色文本。虽然不雅致,但对我的使用效果很好。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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);
}
?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-16 17:42:24

我相信你的目标如下。

  • 要检索单元格中文本部分的字体颜色。
  • 您想要使用googleapis for php来实现这一点。
  • 您已经能够使用Sheets从Google电子表格中获得值。

修改要点:

  • 在您的脚本中,使用了Sheets中的"spreadsheets.values.get“方法。在这种情况下,"textFormat“和"textFormatRuns”的值不包括在内。为了检索单元格中文本部分的值,需要使用"spreadsheets.get“方法。

当以上要点反映到您的脚本中时,如下所示。

修改脚本:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$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);

结果:

当上面的脚本用于以下情况时,

得到了如下结果。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[
  [
    {
      "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,}]
    }
  ]
]
  • 以上述结果为例,发现指数为8~ 15的sample2为红色。
  • 关于上述结果中rgbColor的颜色,您可以在正式文档中看到详细信息。参考

注意:

  • 这是一个简单的示例脚本。因此,请根据您的实际情况修改上述内容。

参考资料:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67557680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文