簡體   English   中英

谷歌表格腳本幫助在表格之間移動數據

[英]Google Sheets script help moving data between sheets

我正在將一個列表加載到谷歌工作表選項卡 SheetA 中,然后將數據單元格逐個修改到不同選項卡 SheetB 中的同一個谷歌工作表中,並在它過去時進行一些操作。

但我知道如何做到這一點的唯一方法是每次都激活工作表,而且它真的很慢。 有沒有辦法移動數據而無需物理激活工作表。 因此,例如,從 SheetA 中獲取單元格 A1 的值,而無需停用 SheetB?

這是代碼部分的示例,您可以在其中看到它來回激活工作表。 我曾經在 Excel 中做過類似的 function 但在 Excel 中我不必激活工作表,我可以在運行時關閉視覺傳輸,這使得整個過程非常快。

你可以在谷歌表格中做同樣的事情嗎? 如果是這樣,語法是什么?

   while (SpreadsheetApp.getActiveSheet().getRange('A'+ COUNTERSheetA).getValue() != ""){
     VALUEA = SpreadsheetApp.getActiveSheet().getRange('A'+ COUNTERSheetA).getValue()
     VALUEB = SpreadsheetApp.getActiveSheet().getRange('B'+ COUNTERA).getValue()
     spreadsheet.setActiveSheet(spreadsheet.getSheetByName('SheetA'), true);
     spreadsheet.getRange('A'+COUNTERSheetB).activate();
     spreadsheet.getCurrentCell().setValue(VALUEA);
     spreadsheet.getRange('B'+COUNTERSheetB).activate();
     spreadsheet.getCurrentCell().setValue(VALUEB);
     COUNTERSheetB = COUNTERSheetB + 1
     COUNTERSheetA = COUNTERSheetA + 1
     spreadsheet.setActiveSheet(spreadsheet.getSheetByName('SheetA'), true);
   }
function myfunc() {
  const ss=SpreadsheetApp.getActive();
  const shA=ss.getSheetByName('SheetA');//gets SheetA by name
  const rgA=shA.getDataRange();//gets all of the data in the sheet
  var vA=rgA.getValues();//gets all the data at one time as one two dimensional array of data
  vA.forEach(function(r,i){
    r.forEach(function(c,j){
      vA[i][j]=(WhateverYouWanttoDoToEachCell);
    });
  });
  const shB=ss.getSheetByName('SheetB');//gets SheetB by name
  const rgB=shB.getRange(1,1,vA.length,vA[0].length).setValues(vA);//1,1 is A1 but you could choose the upper left corner to be anywhere.  Loads all of the data at one time.
  //If you're going to be doing something with those spreadsheet values immediately you may wish to employ SpreadsheetApp.flush() to guarantee that all of the values have been loaded into the spreadsheet.
  SpreadsheetApp.flush();
}

Array.forEach() 方法

電子表格參考

你當然可以。 您可以簡單地將每個工作表聲明為變量,然后使用工作表 class 進行調用。 下面的例子。 考慮到每次使用 getRange() 方法時都會對運行時產生負擔。 更好的方法是一次將所有值放入一個二維數組,然后遍歷該數組,轉換為一個新的二維數組,然后將該二維數組寫入另一張表。

function myFunction() {
 let COUNTERSheetA = 1;
 let COUNTERSheetB = 1;
 let VALUEA;
 let VALUEB;
 const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 const sheetA = spreadsheet.getSheetByName('SheetA');
 const sheetB = spreadsheet.getSheetByName('SheetB');
 
  while (sheetA.getRange('A'+ COUNTERSheetA).getValue() != ""){ 
  VALUEA = sheetA.getRange('A'+ COUNTERSheetA).getValue() 
  VALUEB = sheetA.getRange('B'+ COUNTERSheetA).getValue() 
  sheetB.getRange('A'+COUNTERSheetB).setValue(VALUEA); 
  sheetB.getRange('B'+COUNTERSheetB).setValue(VALUEB);
  COUNTERSheetB = COUNTERSheetB + 1 
  COUNTERSheetA = COUNTERSheetA + 1 
  }
}

如果您想使用它,這是實際工作表: https://docs.google.com/spreadsheets/d/1Gev2KxpX5mPik92Io3eOeF3nKngVcKxnWiNktlHxdDI/edit?usp=sharing

這是一個提取所有值,迭代它們,然后將它們插入新工作表的示例。 如果你能在這里找出你需要的邏輯,這是迄今為止最快的方法。

function myFunction() {
 let VALUES;
 const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 const sheetA = spreadsheet.getSheetByName('SheetA');
 const sheetB = spreadsheet.getSheetByName('SheetB');
 
  VALUES = sheetA.getRange(1, 1,sheetA.getLastRow(), 2).getValues();
  
  for (let row of VALUES){
    for (let val of row){
    //Do your manipulations here if you can.
      Logger.log(val);
    }
  }
  sheetB.getRange(1, 1,sheetA.getLastRow(), 2).setValues(VALUES);
}

這是上述代碼的工作表: https://docs.google.com/spreadsheets/d/1Gev2KxpX5mPik92Io3eOeF3nKngVcKxnWiNktlHxdDI/edit?usp=sharing

暫無
暫無

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

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