简体   繁体   English

Google表格脚本onOpen(e)向表格添加新行

[英]Google Sheets Scripts onOpen(e) Add New Row to Sheet

I have a custom function that inserts a new row under the last non-empty row in a sheet and copies a specific range to this new row. 我有一个自定义函数,可以在工作表中的最后一个非空行下插入新行,并将特定范围复制到该新行。 This functions works fine when I trigger it with a custom menu item. 当我使用自定义菜单项触发它时,此功能可以正常工作。

However, when I add this custom function to the onOpen(e) trigger the script does not work. 但是,当我将此自定义函数添加到onOpen(e)触发器时,脚本不起作用。 I troubleshooted and added an alert in my function - the alert triggers so therefore the function is read in the right place. 我对功能进行了故障排除并添加了警报-警报会触发,因此可以在正确的位置读取该功能。

Please advise how to have my script work onOpen? 请告知我的脚本如何在OpenOpen上运行?

Here's my code for my custom function: 这是我的自定义函数的代码:

function onOpen(e) {
  autoAddRow();
}

function autoAddRow() { 
  var updateFrequency = 3;
  var lRow = getLastRow('A'); 
  var lDate = all.getRange(lRow,1).getValue();  
  var today = new Date();

  Date.prototype.addDays = function(days) {
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
  }
  var newDate = lDate.addDays(updateFrequency);

  if (today >= newDate) {
    var lCol = all.getLastColumn();
    var src = all.getRange(3,1,1,6);
    var dest = all.getRange(lRow+1,1,1,6);

    if(lRow > 20) {
      all.insertRowsAfter(lRow, 1);
    }

    src.copyTo(dest, {contentsOnly:false});
  } else {
    return;
  }
}

So if your example isn't psuedo-code, it wont work. 因此,如果您的示例不是伪代码,它将无法正常工作。 It dies at the second line of your function because the method isn't attached to a class. 它死在函数的第二行,因为该方法未附加到类。

Here is my version of your code (I'm using the first sheet in the spreadsheet app). 这是我的代码版本(我正在使用电子表格应用程序中的第一张纸)。

function autoAddRow() { 
  var updateFrequency = 3

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var data = sheet.getDataRange();  
  var lRow = data.getLastRow();
  var lDate = new Date(sheet.getRange(lRow,1).getValue());
  var today = new Date();
  Logger.log(lDate);
  Logger.log(lRow);
  Logger.log(sheet.getRange(lRow,1).getValue());

  Date.prototype.addDays = function(days) {
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
  }
  var newDate = lDate.addDays(updateFrequency);

  if (today >= newDate) {
    Logger.log("passed if statement");
    var lCol = data.getLastColumn();
    var src = sheet.getRange(lRow,1,1,6);
    var dest = sheet.getRange(lRow+1,1,1,6);

    if(lRow > 20) {
      all.insertRowsAfter(lRow, 1);
    }

    src.copyTo(dest, {contentsOnly:false});
  } else {
    return;
  }
}

The main problems were in the calls to the range and data. 主要问题在于调用范围和数据。 They didn't go anywhere. 他们什么都没去。 The variable lDate needed to be parsed as a date (in my version, my date column was the second column, not the first *edit: changed the code quick to point to your original date cell *end edit), but you have to call the range and get the value to parse it, but after fixing the class issues with regards to getting your data, the rest fell into place. 需要将变量lDate解析为日期(在我的版本中,我的日期列是第二列,而不是第一列* edit:将代码快速更改为指向原始日期单元格* end edit),但是您必须调用范围并获得解析的值,但是在解决了有关获取数据的类问题之后,其余的就到位了。 It even works if you put in in the onOpen. 如果您放入onOpen,它甚至可以工作。

Update: I didn't fix the line 更新:我没有解决这个问题

if(lRow > 20) {
  all.insertRowsAfter(lRow, 1);
}

In fact, I'm not really sure what it does because it looks like it just inserts the row after the last row, which the rest of the function does. 实际上,我不太确定它的作用,因为看起来它只是在最后一行之后插入了该行,其余的功能都这样做了。 But the code 'as is' will break after 20 lines unless you change 'all' to reference the worksheet object. 但是代码“原样”将在20行后中断,除非您更改“全部”以引用工作表对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet Google App脚本onOpen()触发器在工作表上不起作用 - Google App Scripts onOpen() trigger doesn't work on sheets 如何使用谷歌脚本检查两个谷歌工作表,并将重复项移动到新工作表? - How to use google scripts to check two google sheets, and move the duplicates to a new sheet? 如何使用Google表格脚本将Google表格单元格数据添加到html电子邮件中? - How do you add google sheet cell data into an html-email using Google Scripts for Sheets? Google表格脚本,用于将一个表格上的一些单元格值传递到新的表格行 - Google Sheets Script for passing a few cell values on one sheet to new sheet row 谷歌表提交按钮到一个新的表 - google sheets submit button to a new sheet Google Scripts / Sheets 添加前缀国际电话 - Google Scripts / Sheets Add prefix international phone 如何使用 Google App Scripts 在学生考勤表的最后一行添加彩色条纹? - How to add a color stripe to every last row of a student attendance sheet using Google App Scripts? Google表格将一行移动到当前行中2个单元格条件的另一张表格 - Google Sheets move a row to another sheet on 2 cell criteria in current row 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM