簡體   English   中英

谷歌表格剪切和粘貼腳本

[英]Google sheets cut and paste script

全部。

看到很多剪切和粘貼 Google 表格腳本,由於copy from source + paste in destination + delete sourcecut source + paste in destination之間的差異,所有這些腳本都從根本上失敗了。 不同之處在於粘貼的內容引用了與原始內容相同的單元格。 在同一張表中復制會導致在單元格內復制公式,並最終增加其非固定單元格引用的行和列。 將復制的內容粘貼到另一張工作表上會導致上述兩個操作之間的另一個主要區別 - 粘貼的單元格的公式引用來自目標工作表而不是源工作表的值。

在 Google 表格中剪切和粘貼Range作為腳本的正確方法是什么? 我認為只是一個可行的答案,但它是一個網絡請求,而不是 Google Sheets 腳本。 可能可以從表格腳本發送請求,但大概應該有一種更集成的方式來進行剪切和粘貼。 可能 HTTP 請求方式適用於外部應用程序。

在 Google Apps 腳本上,您可以通過高級服務使用表格 API。 要啟用它,請從腳本編輯器菜單中單擊Resources>Advanced Google services...>Google Sheets API

一旦激活它,您就可以使用此處列出的相同端點。

樣本

  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheetId_1 = ss.getSheetByName("source").getSheetId();
  let sheetId_2 = ss.getSheetByName("destination).getSheetId();
  // Using the Sheets API to create the request body
  let request = Sheets.newCutPasteRequest();
  let source = {
    startColumnIndex : 0,
    endColumnIndex : 4,
    startRowIndex : 0,
    endRowIndex : 10,
    sheetId : sheetId_1
  }
  let destination = {
    columnIndex : 5,
    rowIndex : 0,
    sheetId : sheetId_2
  }
  request.source = source;
  request.destination = destination;
  request.pasteType = "PASTE_NORMAL";
  // Calling the Sheets API "batchUpdate" endpoint
  Sheets.Spreadsheets.batchUpdate({"requests": [{"cutPaste" : request }]}, ss.getId());

參考

高級谷歌服務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM