如何在php中使用googleheet api v4中的datetimerenderoption?

问题描述:

我使用V4 API在PHP如何在php中使用googleheet api v4中的datetimerenderoption?

$range = $sheetName; 
$response = $service->spreadsheets_values->get($spreadsheetId, $range); 
$values = $response->getValues(); 
$lastRow = count($values); 
$rowIndex = $lastRow; 
$columnIndex = 0; 

$requests = array(); 
$requests[] = new Google_Service_Sheets_Request(array(
    'updateCells' => array(
     'start' => array(
      'sheetId' => $sheetId, 
      'rowIndex' => $rowIndex, 
      'columnIndex' => $columnIndex 
     ), 
     'rows' => array(
      array(
       'values' => array(
        array(
         'userEnteredValue' => array('numberValue' => '111111'), 
         'userEnteredFormat' => array('backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8)) 
        ), 
        array(
         'userEnteredValue' => array('stringValue' => 'aaaaaaaaa'), 
         'userEnteredFormat' => array('backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8)) 
        ), 
        array(
         'userEnteredValue' => array('numberValue' => '2015-05-05'), 
         'userEnteredFormat' => array('numberFormat' => array('type'=>'DATE', 'pattern'=>'yyyy-mm-dd'), 'backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8)) 
        ) 
       ) 
      ) 
     ), 
     'fields' => 'userEnteredValue,userEnteredFormat.backgroundColor' 
    ) 
)); 

$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
    'requests' => $requests 
)); 

try { 
    $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest); 
} catch(Exception $e) { 
    print_R($e); 
} 

我使用上面的代码做任务写在谷歌片3列,我面对的唯一问题是,同时插入日期,我得到一个错误。

我读了datetimerenderoption是用来插入日期,但我没有得到如何做到这一点。

+0

我已经重新格式化了您的代码,但知道**您得到了什么**错误以及您用datetimerenderoption尝试了什么会很有用。 –

+0

我只是想感谢您提供一个体面的例子,说明如何在Google表格中创建/处理请求!我发现API文档不是特别有用。 – Antony

DateTimeRenderOption用于values collection API(也仅用于阅读,而不是写作)。您正在使用更为简单的spreadsheets collection API,并且不使用该便利选项。

如果你粘贴你收到的错误会更容易帮助,但我假设它与设置numberValue的行有关。 docs say numberValue接受一个数字,而不是一个字符串。如果您想设置日期,则需要提供该日期的号码Serial Number format。您可以查看this SO answer以获得有关处理序列号的好建议。

+0

错误发生 - 'requests [0] .update_cells.rows [0] .values [2] .user_entered_value.number_value'(TYPE_DOUBLE),“2016-05-05” –

+0

上的值无效是的,这是我的错误认为是问题。你正在传递一些想要数字的东西。看到我上面的答案。 –

+0

我没有得到如何在PHP中设置序列号格式。请帮忙 –