簡體   English   中英

如何復制每月增加 1 列的工作表中的最后 3 列

[英]How to copy the last 3 columns in a sheet that grows by 1 column each month

我需要一個始終只包含 sheet1 的最后 3 列的 sheet2。

Sheet1 從列 A、B、C、D、E、F、G 開始,然后每個月增加一列。

所以 Sheet2 不會增長......每個月最后三列將被覆蓋

我唯一能找到的是如何獲得最后一列

我相信你的目標如下。

  • 您想要從工作表的數據范圍中檢索最后 3 列。
  • 您希望使用 Google Apps 腳本實現此目的。

在這種情況下,下面的示例腳本怎么樣?

示例腳本:

function myFunction() {
  const sheetName = "Sheet1"; // Please set your sheet name.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName(sheetName);
  const values = srcSheet.getDataRange().getValues().map(r => r.splice(-3));
  console.log(values); // You can confirm the retrieved values in the log.
}
  • 運行此腳本時,將從“Sheet1”的數據范圍中檢索最后 3 列。

  • 如果您想檢索最后一列而不是最后三列,請將r.splice(-3)修改為r.splice(-1)

參考:

添加:

關於您的以下附加問題,

如何將其粘貼到“sheet2”列 EFG 中?

在這種情況下,下面的示例腳本怎么樣?

function myFunction() {
  const sheetName = "Sheet1"; // Please set your sheet name.
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName(sheetName);
  const values = srcSheet.getDataRange().getValues().map(r => r.splice(-3));
  console.log(values); // You can confirm the retrieved values in the log.

  // Added
  const dstSheet = ss.getSheetByName("Sheet2");
  dstSheet.getRange(1, 5, values.length, values[0].length).setValues(values);
}
  • 通過此修改, values從“E”列中輸入。

嘗試這個:

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("SheetName");
  const lc = sh.getLastColumn();
  const vs = sh.getRange(1,lc - 2,sh.getLastRow(),3).getValues();
  console.log(vs); 
}

暫無
暫無

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

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