2つのプレッドシートを相互参照し、行列の挿入削除を含めて同期してデータを編集したい

実現したいこと

2つのプレッドシートを相互参照し、行列の挿入削除を含めてデータを編集したい。

前提

2つのスプレッドシートAとBの間で相互参照をしたいです。
お互いにセルの編集するところまではできた(ネットで見つけたコードを参考にした)のですが、行列の挿入削除がうまくできません。

ネットで見つけたコードはシートを全選択していましたが、私のほうでは部分的に選択(AスプシのデータをBスプシが部分的に参照している)しています。

発生している問題・エラーメッセージ

行列の挿入削除を編集と同じようなコードで記述すると、表が崩れてしまい使い物になりません。
Bスプシで行を挿入してもAスプシに反映されない列がある(Bスプシから参照していない列には挿入されない)、Bスプシで行挿入ではなくセル編集をしただけなのにAスプシに部分的に行が挿入されてしまうなど、めちゃくちゃになってしまいます。

該当のソースコード

GAS,JavaScript

1const sourceID = "XXX": 2const sourceSheet = "テストX"; 3const copyID = "YYY"; 4const copySheet = "テストY"; 5 6function importUsername() { 7 const thisSpreadsheet = SpreadsheetApp.openById(sourceID); 8 const thisSheet = thisSpreadsheet.getSheetByName(sourceSheet); 9 const lastRow = thisSheet.getLastRow(); 10 const thisData = thisSheet.getRange(4, 1, lastRow, 1); 11 12 const toSpreadsheet = SpreadsheetApp.openById(copyID); 13 const toSheet = toSpreadsheet.getSheetByName(copySheet); 14 const toRange = toSheet.getRange(4, 1, lastRow, 1); 15 toRange.setValues(thisData.getValues()); 16} 17 18function importData1(){ 19 const thisSpreadsheet = SpreadsheetApp.openById(sourceID); 20 const thisSheet = thisSpreadsheet.getSheetByName(sourceSheet); 21 const lastRow = thisSheet.getLastRow(); 22 const thisData = thisSheet.getRange(4, 10, lastRow, 2); 23 24 const toSpreadsheet = SpreadsheetApp.openById(copyID); 25 const toSheet = toSpreadsheet.getSheetByName(copySheet); 26 const toRange = toSheet.getRange(4, 2, lastRow, 2); 27 toRange.setValues(thisData.getValues()); 28} 29 30function insertRow(){ 31 const thisSpreadsheet = SpreadsheetApp.openById(sourceID); 32 const thisSheet = thisSpreadsheet.getSheetByName(sourceSheet); 33 const myActiveCell = thisSheet.getActiveCell(); 34 const selectedRow = myActiveCell.getRow(); 35 36 const toSpreadsheet = SpreadsheetApp.openById(copyID); 37 const toSheet = toSpreadsheet.getSheetByName(copySheet); 38 toSheet.insertRows(selectedRow); 39 40} 41

試したこと

import~のほうはネットで見つけたコードで、これ自体(セルの相互編集)は問題なくできました。
問題はinsertRowのほうで、insertRows系を全種類を変えてやってみたのですが、期待通りに動いてくれません。どれも表示が崩れてしまいます。
ちなみにすべてトリガーの「編集時」を利用しています。

もしかしてスプシではこういったことは実現不可能なのでしょうか?
ネットで検索しても、相互参照のコードは発見できましたが、行列挿入削除を同期するようなコードは見当たりませんでした。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

コメントを投稿

0 コメント