ReferenceError:未定义“ getLastPopulatedRow”

时间:2018-12-13 15:56:01

标签: google-sheets

晚安

我正在google上创建电子表格,目的是自动化数据的存储和管理。为此,人们在“条目”选项卡中填写数据,我需要开发一个脚本将这些数据启动到“数据库”数据库中。

Image1

在此图像中显示了皮瓣人员的填写。我的想法是,在填充的末尾有一个按钮,信息将被发送到数据库,如下所示:

Image2

我将颜色表示应复制到“条目”中并粘贴到“ BD”中的列

我试图创建以下脚本:

function moveValuesOnly() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getRange('Entradas!B9:J29');
  var destSheet = ss.getSheetByName('BD');
  var lastFilledRowInColumnA = getLastPopulatedRow(destSheet.getRange('A:A').getValues());
  var destRange = destSheet.getRange(lastFilledRowInColumnA+1,1);
  source.copyTo(destRange, {contentsOnly: true});
  source.clear();
};

但是,我无法复制和粘贴信息。

能帮我吗?我已经尝试了两个多月,但似乎无法解决问题。

点击指向电子表格模板的链接: https://docs.google.com/spreadsheets/d/1BuLBU2qKB8ZNMi_wVvTHwW6MyODDTKPoP5Imn1r1L7Q/edit?usp=sharing

1 个答案:

答案 0 :(得分:0)

getlastrow命令撤消了OP原始代码。我已经通过查看特定的数据列(Entradas列B和BD列A)并使用length命令来获取准确的行数来代替它。我还在代码中留下了一系列Logger命令,以便OP可以在需要时检查键值。

我使用copyValuesToRangedocumentation)命令复制数据,并使用clear命令(OP使用,但使用contentsOnly({{ 3}})选项。

最后,OP应该将工作表BD,列A的格式设置为日期,并从BD中删除所有数据验证。

function movedata() {

  // setup basics for spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // source and destination sheets
  var sourceSheet = ss.getSheetByName("Entradas");
  var destSheet = ss.getSheetByName('BD');

  // calculate the last row on the source
  var Sourcevals = sourceSheet.getRange("B9:B").getValues();
  var Sourcelast = Sourcevals.filter(String).length;
  //Logger.log("DEBUG: last row in Source = " + Sourcelast);//DEBUG

  //if there is no data to copy, then stop the code.
  if (Sourcelast ==0){
    Logger.log(" there is no data - stop the code");
    return;
  }

  // define the Source Range and get the Source data  
  var sourceRange = sourceSheet.getRange(9, 2, Sourcelast, 9);
  // Logger.log("DEBUG: source range: " + sourceRange.getA1Notation());// DEBUG
  var sourceData = sourceRange.getValues();

  // calculate the last row on the destination
  var Destvals = destSheet.getRange("A1:A").getValues();
  var Destlast = Destvals.filter(String).length;
  // Logger.log("DEBUG: last row in column A = " + Destlast);// DEBUG

  // copy the data fro Source to destination
  sourceRange.copyValuesToRange(destSheet, 1, 11, Destlast + 1, Destlast + 1 + Sourcelast);
  // delete the data from the Source
  sourceRange.clear({contentsOnly: true});

}
相关问题