Google表格脚本可帮助在表格之间移动数据

时间:2020-06-22 21:26:13

标签: google-apps-script google-sheets

我正在将一个列表加载到Google工作表标签SheetA中,然后将数据单元格逐个修改到另一个标签SheetB中相同的Google工作表中,并对其进行一些操作。

但是我唯一知道的方法是每次都激活工作表,而且速度真的很慢。有没有一种方法可以移动数据而无需物理激活工作表。因此,为了轻松掌握SheetA中A1单元格的值而无需停用SheetB吗?

这是代码部分的示例,您可以在其中看到它来回激活工作表。我曾经在Excel中执行过类似的功能,但是在Excel中我不必激活工作表,并且可以在运行时关闭视觉效果,从而使整个传输过程非常快。

您可以在Google表格中这样做吗?如果是这样,语法是什么?

   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);
   }

2 个答案:

答案 0 :(得分:2)

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() method

Spreadsheet Reference

答案 1 :(得分:1)

您肯定可以。您可以简单地将每个工作表声明为变量,然后使用工作表类进行调用。下面的例子。 考虑到每次使用getRange()方法都会对运行时造成负担。 更好的方法是一次将所有值放入2D数组中,然后遍历该数组,转换为新的2D数组,然后将该2D数组写入另一张纸。

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

相关问题