0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

spreadsheet APIで値を入力する方法 - Android Kotlin

Posted at
  val sheets = Sheets.Builder(
      NetHttpTransport(),
      GsonFactory.getDefaultInstance(),
      HttpRequestForGoogle(accessToken)
  )
      .setApplicationName("任意のアプリケーション名")
      .build()
      
  val values = ValueRange().setValues(listOf(listOf("test")))
  
  
  sheets.spreadsheets().values().update(
      spreadsheetId,
      "更新したいシートの範囲", // 例: "シート1!A1:A1"
      values,
  )
      .setValueInputOption("USER_ENTERED")
      .execute()

引っ掛かりポイントとしては「setValueInputOption」の設定が必須である部分です。設定を忘れると下記エラーが出力されます。

400 Bad Request
PUT https://sheets.googleapis.com/v4/spreadsheets/指定したid/values/指定した範囲
{
  "code": 400,
  "errors": [
    {
      "domain": "global",
      "message": "'valueInputOption' is required but not specified",
      "reason": "badRequest"
    }
  ],
  "message": "'valueInputOption' is required but not specified",
  "status": "INVALID_ARGUMENT"
}

ValueInputOptionに設定できる値は公式にもありますが、下記に和訳を置いておきます。

Enums Enumsの訳 詳細
USER_ENTERED ユーザー入力 UI操作でスプレッドシートに値を入力した場合と同じように、入力した値によってデータの型を変更します。例えば、文字列「123」は数字として入力されます。
RAW 修正なし USER_ENTEREDのようにデータの型を変更せずに、そのままで値を入力します。

注意
・公式には「INPUT_VALUE_OPTION_UNSPECIFIED」がありますが、説明欄には「使用しないでください」の表記があるため表には載せていません。
・読みやすさのため、表のアイテムの順番を入れ替えています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?