如何在Google电子表格API中设置验证方法 [英] How to set validation method in Google spreadsheets API

查看:70
本文介绍了如何在Google电子表格API中设置验证方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对新的Google Sheets API v4感到困惑.我的问题是:如何为电子表格中的指定列设置验证规则? 没有说明如何使用适当方法的有用教程.

I am confused about the new Google Sheets API v4. My question is: how can I set validation rules for specified column(s) in spreadsheet? There is no useful tutorial with description how to use appropriate methods.

结果应类似于以下示例:

Result should look like following sample:

应该在数据上传之前设置验证(工作正常).

That validation should be set before data upload (which works fine).

我当前的代码:

$client = getClient();
$service = new Google_Service_Sheets($client);
$fileId = 'my-document-id';

$body = new Google_Service_Sheets_SetDataValidationRequest(array(
    'setRange' => new Google_Service_Sheets_GridRange(
        array(
            'sheetId'=>'List1',
            'startColumnIndex'=>'0',
            'endColumnIndex'=>'1'
        )
    ),
    'setRule' => new Google_Service_Sheets_DataValidationRule(
        array(

            'setValues'=>array('YES','NO')
        )
    )
));


$sheetReq = new Google_Service_Sheets_Request($client);
$sheetReq->setSetDataValidation($body);


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


$result = $service->spreadsheets->batchUpdate($fileId, $batchUpdateRequest);

推荐答案

感谢@ random-parts的帮助,它使我走上了正确的轨道.如果其他人将尝试在功能中解决PHP中的类似问题,请找到以下完全正常的示例:

Thank you @random-parts for help, it has brought me to the right track. If someone else will try to solve similar problem in PHP in feature, please find bellow fully working example:

    $client = $this->getClient();
    $service = new Google_Service_Sheets($client);
    $ary_values = ['yes','nope','maybe','never ever'];

    foreach( $ary_values AS $d ) {
        $cellData = new Google_Service_Sheets_ConditionValue();
        $cellData->setUserEnteredValue($d);
        $values[] = $cellData;
    }

    $conditions = new Google_Service_Sheets_BooleanCondition();
    $conditions->setType('ONE_OF_LIST');
    $conditions->setValues($values);

    $setRule= new Google_Service_Sheets_DataValidationRule();
    $setRule->setCondition($conditions);
    $setRule->setInputMessage('Please set correct value');
    $setRule->setShowCustomUi(true);

    $range = new Google_Service_Sheets_GridRange();
    $range->setStartRowIndex(1);
    $range->setEndRowIndex(5);
    $range->setStartColumnIndex(1);
    $range->setEndColumnIndex(2);
    $range->setSheetId(YOUR_SHEET_ID); //replace this by your sheet ID

    $valReq = new Google_Service_Sheets_SetDataValidationRequest();
    $valReq->setRule($setRule);
    $valReq->setRange($range);

    $sheetReq = new Google_Service_Sheets_Request();
    $sheetReq->setSetDataValidation($valReq);

    $bodyReq = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
    $bodyReq->setRequests($sheetReq);

    $result = $service->spreadsheets->batchUpdate($fileId, $bodyReq);

这篇关于如何在Google电子表格API中设置验证方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆