0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Sheets API v4「1セル 50000文字までの制限あるよ」

Last updated at Posted at 2019-04-08

@koshi_life です。

タイトルのままです。

スプレッドシートの1セルに50000文字以上の値を書き込む可能性があるなら、
スプレッドシートに書き込む前に50000文字以内にしてから書き込む もしくは エラーキャッチするなりしようという話です。
エラーメッセージも日本語でわかりやすく表示されていますが、公式ドキュメントに見当たらない制約だったので備忘します。

スタックトレース

$ bundle exec ruby update_sheets.rb 
Traceback (most recent call last):
        16: from update_sheets.rb:18:in `<main>'
        15: from [project]/vendor/bundle/ruby/2.6.0/gems/google_drive-3.0.2/lib/google_drive/worksheet.rb:425:in `s
ave'
        14: from [project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/generated/google/apis/sheets_v4/s
ervice.rb:789:in `update_spreadsheet_value'
        13: from [project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/lib/google/apis/core/base_service
.rb:360:in `execute_or_queue_command'
        12: from [project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/lib/google/apis/core/http_command
.rb:102:in `execute'
        11: from [project]/vendor/bundle/ruby/2.6.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
        10: from [project]/vendor/bundle/ruby/2.6.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
         9: from [project]/vendor/bundle/ruby/2.6.0/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable
'
         8: from [project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/lib/google/apis/core/http_command
.rb:110:in `block in execute'
         7: from [project]/vendor/bundle/ruby/2.6.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
         6: from [project]/vendor/bundle/ruby/2.6.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
         5: from [project]/vendor/bundle/ruby/2.6.0/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
         4: from [project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/lib/google/apis/core/http_command.rb:113:in `block (2 levels) in execute'
         3: from [project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/lib/google/apis/core/http_command.rb:310:in `execute_once'
         2: from [project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/lib/google/apis/core/http_command.rb:193:in `process_response'
         1: from [project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/lib/google/apis/core/api_command.rb:116:in `check_status'
[project]/vendor/bundle/ruby/2.6.0/gems/google-api-client-0.28.6/lib/google/apis/core/http_command.rb:228:in `check_status': badRequest: 入力内容が 1 つのセルに最大 50000 文字の制限を超えています。 (Google::Apis::ClientError)

検証コード

update_sheets.rb
require 'google_drive'

# 指定の文字列長の文字列を生成します。
def generate_rand_text(length: 100)
  text = []
  (1..length).each { |index| text << index % 10 }
  text.join('')
end

session = GoogleDrive::Session.from_config('config.json')

sheets_id = 'xxx_xxx_xxx'
sheets = session.spreadsheet_by_key(sheets_id)

worksheet = sheets.worksheets[0]
value = generate_rand_text(length: 50001) # NG
#value = generate_rand_text(length: 50000) # OK
#value = generate_rand_text(length: 50001)[0, 50000] # OK (50000文字以降は切り捨てればOK)
worksheet[1, 1] = value
worksheet.save

google-drive-ruby ライブラリの使い方は前回記事を参照。
RubyでGoogle Drive/スプレッドシートを扱う

参考

GoogleAppsScript(GAS)でのcacheあれこれ

にも制限に関する記述あり。

Cache.gs
// ※1セル50000文字までという制限があるため

0
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?