テーブルデータの内容をクリップボードへコピーしたい

上記のようなテーブルフィールドがあり、レコード登録完了時にテーブルのヘッダーと計算部分を除く以下項目

       

9:11

 

|

17:59

 

|

test

 

|

 

 

|
|

17:55

 

|

18:56

 

|

はしる

 

|

 

 

|
|

14:56

 

|

14:59

 

|

たべる

 

|

 

 

|
|

15:02

 

|

15:42

 

|

あるく

 

|

 

 

|
|

13:00

 

|

14:58

 

|

たべる

 

|

 

|

を、クリップボードへコピーしたいです。タイミングについてはボタンを設置してクリック時にコピーするような事が出来ればと思います。
https://developer.cybozu.io/hc/ja/community/posts/360038857331-%E3%83%9C%E3%82%BF%E3%83%B3%E3%82%AF%E3%83%AA%E3%83%83%E3%82%AF%E3%81%A7%E3%82%AF%E3%83%AA%E3%83%83%E3%83%97%E3%83%9C%E3%83%BC%E3%83%89%E3%81%AB%E3%82%B3%E3%83%94%E3%83%BC
こちらを導入してみたのですが、うまくいかず。

gsc-hnd-dadさん

こんにちは。

デザインを拝見し、スペースフィールド等に自作されたテーブルと勝手に判断しましたので、それを前提にスクリプトを書いてみます。

テーブルが、kintone標準のサブテーブルフィールドでしたら、少し書き方が異なります。

 

クリップボードへコピーするfunction

function ClipboardTable() {

 var str = '';
 var table = document.getElementById('tableField'); // テーブル要素のIDに変えてください。
 var tr = table.rows;

 // テーブル行に対して処理(ヘッダー行は除く)
 for (var i = 1; i < tr.length; i++) {
  var td = table.rows[i].cells;
  var tr_Text = '';

  // テーブル列に対して処理(最終列は除く)
  for (var j = 0; j < td.length - 1; j++) {
   if (tr_Text != '') {
    tr_Text += '\t';
   }
   tr_Text += td[j].innerText;
  }

  // 行末の改行
  str += tr_Text + '\r';
 }

 // テキストエリアの一時作成
 var tempText = document.createElement('textarea');
 tempText.value = str;
 document.body.appendChild(tempText);

 tempText.select();

 // クリップボードへコピー
 var res = document.execCommand('copy');

 // テキストエリアの削除
 document.body.removeChild(tempText);

 // 結果メッセージ
 if (res) {
  alert('クリップボードへのコピーに成功しました。');
 } else {
  alert('クリップボードへのコピーに失敗しました。');
 }

}

 

ボタン(クリック時にクリックボードへコピー)

var clipboardButton = document.createElement('button');
clipboardButton.id = 'btn-clipboard';
clipboardButton.type = 'button';
clipboardButton.innerText = 'クリップボード';
clipboardButton.onclick = function() {
 ClipboardTable();
};
document.getElementById('buttonField').appendChild(clipboardButton);

 

ご返信ありがとうございます。

テーブルに関しては標準のサブテーブルになります。
ご教授お願い致します。

サブテーブルでしたら、下記の通りです。

フィールド名は適宜変更ください。

function ClipboardTable(event) {

 var record = event.record;

 var str = '';
 var table = record.テーブル.value;

 // テーブル行に対して処理(ヘッダー行は除く)
 for (var i = 0; i < table.length; i++) {
  var tr_Text = '';
  tr_Text += table[i].value.開始時間.value + '\t';  // タブ
  tr_Text += table[i].value.終了時間.value + '\t';  // タブ
  tr_Text += table[i].value.作業内容.value + '\r'; // 改行
  str += tr_Text;
 }

 // テキストエリアの一時作成
 var tempText = document.createElement('textarea');
 tempText.value = str;
 document.body.appendChild(tempText);

 tempText.select();

 // クリップボードへコピー
 var res = document.execCommand('copy');

 // テキストエリアの削除
 document.body.removeChild(tempText);

 if (res) {
  alert('クリップボードへのコピーに成功しました。');
 } else {
  alert('クリップボードへのコピーに失敗しました。');
 }

}


// レコード追加画面保存成功時
kintone.events.on('app.record.create.submit.success', function(event) {

 ClipboardTable(event);

 return event;

});

無知故に応用が利かずスミマセン。

ボタン作成~クリップボードへコピーまで記述する為にどう記述したらよろしいでしょうか。

レコード保存後の詳細画面表示時にボタンを表示するスクリプトです。

サブテーブルの下などにスペースフィールド「buttonField」を設置してください。

先にコメントした「レコード追加画面保存成功時」以下を下記に変更してください。

// レコード詳細画面表示時
kintone.events.on('app.record.detail.show', function(event) {

 var clipboardButton = document.createElement('button');
 clipboardButton.id = 'btn-clipboard';
 clipboardButton.type = 'button';
 clipboardButton.innerText = 'クリップボード';
 clipboardButton.onclick = function() {
  ClipboardTable(event);
 };
 kintone.app.record.getSpaceElement('buttonField').appendChild(clipboardButton);

 return event;

});

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。