2017-07-31 3 views
2

シートの先頭(先頭)に新しいデータを追加します。だから私はシートに新しい列A1を追加する必要があります。しかし、私はPHPでAPIの例を見つけることができません。Googleスプレッドシートに列を挿入するにはどうすればよいですか?

$body = new Google_Service_Sheets_ValueRange(['values' => $values]); 
$result = $service->spreadsheets_values->append($new_sheet_id, 'A1:E1', $body, $options); // appent the data to the spreadsheet 

ありがとう:

今、私はこれを使用してデータを追加しています。

UPD: ここでは、私は私はそれがスプレッドシートのAPIでは不可能であると信じて

/* insert columns */ 
$requests = new Google_Service_Sheets_Request(array(
    'insertDimension' => array(
     'range' => array(
      'sheetId' => 0, 
      'dimension' => "COLUMNS", 
      'startIndex' => 0, 
      'endIndex' => 5 
     ) 
    ) 
)); 
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
    'requests' => $requests 
)); 
$result = $service->spreadsheets->batchUpdate($new_sheet_id, $batchUpdateRequest); 
/* insert columns */ 

答えて

0

を発見したものです。私はその可能性を勉強してきましたが、これまでどのようにしているのか分かりません。代わりの解決方法がありますが、Google Apps Script Execution APIを使用する必要があります。ソリューションが適切であれば自由に実装してください。次の手順に従います。

  1. 新しいスプレッドシートを作成し、任意の名前を付けます。
  2. それは名前「ツール>スクリプトエディタ」
  3. に行くことによって、スクリプトエディタを開き
  4. enter image description here 下の画像と同じに見えるように、あなたは下の画像の中に見えるようしたいプロジェクト何かをスプレッドシートに必要事項を記入 enter image description here
  5. "Code.gs" の内側にあなたが持っているeverytingを削除し、次のように貼り付けます。

    function insertColumn(vals) { 
    
        var ss = SpreadsheetApp.getActiveSpreadsheet(); 
        var sheet = ss.getSheets()[0]; 
    
        // This inserts a column in the first column position 
        sheet.insertColumnBefore(1); 
    
        //This set the values in the cells of the first column inserted 
        var range = sheet.getRange("A1:A5"); 
        range.setValues(vals); 
    
    } 
    
  6. 、APIの男に行きますAGER、プロジェクトを選択し、APIを有効にする、 enter image description here

  7. ゴー下の画像のように、あなたのプロジェクト設定へとへ enter image description here

  8. 戻り、以下の画像のようにプロジェクト番号をコピー"リソース>クラウドプラットフォームプロジェクト"をクリックしてください。

  9. 「プロジェクト番号を入力してください」というフィールドにプロジェクト番号を貼り付け、「プロジェクトの設定」をクリックします。下の画像を参照してください。 enter image description here

  10. 確認ウィンドウが表示されます。変更を確認し、完了するのを待ちます。

  11. プロジェクトエディタ内で、[Publish]> [Deploy as API Executable]に移動します。バージョンを入力し、 "Deploy"をクリックしてください。 enter image description here

  12. 現在のAPIのIDをコピーして、プロジェクトを保存することを確認してください:それはあなたの情報メッセージの多くのカップルが表示され、その後、あなたがこれを見るべき enter image description here

  13. 下の画像を参照してください。変更。

  14. 新しいPHPファイルを作成し、次を貼り付けます。

    <?php session_start(); 
    
    //INCLUDE PHP CLIENT LIBRARY 
    require_once 'vendor/autoload.php'; 
    
    //scope required to modify the spreadsheet 
    $scopes = array("https://www.googleapis.com/auth/spreadsheets"); 
    
    // Create client object 
    $client = new Google_Client(); 
    $client->setRedirectUri('http://'.$_SERVER['HTTP_HOST'].'/index.php'); 
    $client->setAuthConfig("client_secret.json"); 
    $client->setScopes($scopes); 
    
    if(isset($_SESSION["access_token"]) && ($_SESSION["access_token"])) { 
    
        $client->setAccessToken($_SESSION["access_token"]);  
        $service = new Google_Service_Script($client); 
    
        //Here goes the script Id you copied on step 13 
        $scriptId = 'XXX-OROcbUXO78URUxxxLYqFdOk6teXXX'; 
    
        $values = array( 
         array("Email"), 
         array("[email protected]"), 
         array("[email protected]"), 
         array("[email protected]"), 
         array("[email protected]") 
        ); 
    
        $params = array($values); 
    
        // Create an execution request object. 
        $request = new Google_Service_Script_ExecutionRequest(); 
        $request->setFunction('insertColumn'); 
        $request->setParameters($params); 
        $request->setDevMode(true); //required to work with parameters 
    
        try { 
        // Make the API request. 
         $response = $service->scripts->run($scriptId, $request); 
    
         if ($response->getError()) { 
         // The API executed, but the script returned an error. 
    
         // Extract the first (and only) set of error details. 
         // The values of this object are the script's 'errorMessage' 
         // and 'errorType', and an array of stack trace elements. 
         $error = $response->getError()['details'][0]; 
         printf("Script error message: %s\n", $error['errorMessage']); 
    
          if (array_key_exists('scriptStackTraceElements', $error)) { 
           // There may not be a stacktrace if the script didn't start executing. 
           print "Script error stacktrace:\n"; 
           foreach($error['scriptStackTraceElements'] as $trace) { 
            printf("\t%s: %d\n", $trace['function'], $trace['lineNumber']); 
           } 
          } 
    
         } else { 
         // The structure of the result will depend upon what the Apps Script 
         // function returns. 
         $resp = $response->getResponse();  
         var_dump($resp);   
    
         } 
        } catch (Exception $e) { 
         // The API encountered a problem before the script started executing. 
         echo 'Caught exception: ', $e->getMessage(), "\n"; 
        }  
    
    } else { 
    
        if(!isset($_GET["code"])){ 
    
         $authUrl = $client->createAuthUrl(); 
         header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL)); 
    
        } else { 
    
         $client->authenticate($_GET['code']); 
         $_SESSION['access_token'] = $client->getAccessToken(); 
    
         $redirect_uri = 'http://'.$_SERVER['HTTP_HOST'] .'/index.php'; 
         header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); 
    
        } 
    
    } 
    
    ?> 
    
  15. は、PHPスクリプトを実行して、あなたのSpreadhsheetに結果を見てください。以下のように表示されます。 enter image description here

あなたが見ることができるように、列を先頭に挿入し、値があまりにも記入されました。私はこれが何とかあなたや他の人に役立つことを願っています。

関連する問題