我得到了一个';谷歌请求中的错误';(400)对于某些行使用电子表格API时出错,但对于大多数行则不是


I get a 'Error in Google Request' (400) error using spreadsheet API for some rows, but not for most

我有一个网页,它发布了将谷歌电子表格单元格更新为php脚本的请求。它使用了用于google电子表格API的asimlqt/php-google-spreadsheet-client/php包装器。大多数时候,它运行良好。不过,对于一些单元格,它会给我一个"谷歌请求中的错误"错误。我调查了一下,发现返回的http_code是400。

我不知道更新不起作用的单元格有什么不同。它们保存通过谷歌表单输入的自由格式文本,但有问题的单元格不是文本最长的单元格,也没有任何奇怪的字符。有100多行,我只发现了几个出现这个问题的地方,到目前为止,我只看到了一列中单元格的问题,所以我很确定凭据是可以的。

错误来自功能:

function updateCell($row, $col, $val) {
    global $cellFeed;
    try {
        $cellFeed->editCell($row+2, $col+1, $val);
    } catch (Exception $e) {
        output('ERROR (updateCell): ' . $e->getMessage());
        return false;
    }
    return true;
}

CCD_ 1来自

$serviceRequest = new Google'Spreadsheet'DefaultServiceRequest($accessToken);
Google'Spreadsheet'ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google'Spreadsheet'SpreadsheetService();
$spreadsheet = $spreadsheetService->getSpreadsheetById($SPREADSHEET_ID);
$worksheetFeed = $spreadsheet->getWorksheets();
$worksheet = $worksheetFeed->getByTitle($WORKSHEET_NAME);
$cellFeed = $worksheet->getCellFeed();

当单元格值中有双引号时,我也遇到过同样的错误。它是通过用&qout;替换它们来修复的;。

这是因为CellFeed.php中的这段代码:

public function editCell($rowNum, $colNum, $value)
{
    $entry = sprintf('
        <entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">
          <gs:cell row="%u" col="%u" inputValue="%s"/>
        </entry>',
        $rowNum,
        $colNum,
        $value
    );
    ServiceRequestFactory::getInstance()->post($this->getPostUrl(), $entry);
}

正如您所看到的,这里没有$value的转换,所以当有双引号时,XML标记就会损坏,比如:

<gs:cell row="%u" col="%u" inputValue="my value with "quotes" in it."/>