doubingqi5829 2017-02-19 09:46
浏览 73
已采纳

如何使用Google表格API v4使用PHP在电子表格中创建新工作表或选项卡

With PHP, it is unclear from the Google Sheets API v4 documentation on how to create a new sheet (aka "tab") in an existing spreadsheet.

I can do it with batchUpdate, oddly, via the API Explorer, but they don't explain from that how to do this in PHP.

  • 写回答

1条回答 默认 最新

  • dqhbuwrwq692118284 2017-02-19 10:26
    关注

    It looks like the documentation is saying that we must use batchUpdate from $service->spreadsheets_values collection. But that's incorrect. It should be the $service->spreadsheets collection. The proper answer after a lot of trial and error is:

    try {
        $body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
            'requests' => array(
                'addSheet' => array(
                    'properties' => array(
                        'title' => 'New Sheet'
                    )
                )
            )
        ));
        $result1 = $service->spreadsheets->batchUpdate($sSpreadsheetID,$body);
    } catch(Exception $ignore) {}
    

    Given that you have already authenticated your API to get a $client object, and then used that to get a $service object of class Google_Service_Sheets, and given that you have assigned $sSpreadsheetID to the ID of your spreadsheet, then the above routine will attempt to add a new tab to your spreadsheet named 'New Sheet' without destroying any existing one, and will not show errors if this tab already exists. At that point, you can do something new with that new sheet by addressing it with the A1 Notation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图