简体   繁体   English

关于电子表格和工作表的新手问题

[英]Newbie question about Spreadsheet and Sheet

It is wrong to use spreadsheet and sheet like this?像这样使用电子表格和工作表是错误的吗?

function ADD_COLUMN() {
  var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('B:B').activate();
  spreadsheet.getActiveSheet().insertColumnsBefore(spreadsheet.getActiveRange().getColumn(), 1);
  spreadsheet.getActiveRange().offset(0, 0, spreadsheet.getActiveRange().getNumRows(), 1).activate();
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    sheet.getRange(1,2).setValue(sheet.getRange(1,3).getValue()+1);
    var addedDateAndTime = Utilities.formatDate(new Date, ss.getSpreadsheetTimeZone(), "MMMM yyyy");
    sheet.getRange(2,2).setValue(addedDateAndTime)
};

This script adds a column between A and B columns, inserts the value of (C1 --This cell is B1 before the bew column is created-- + 1) into B1 and and the current date (month and year) into B2.此脚本在 A 和 B 列之间添加一列,将 (C1 --This cell is B1 before the bew column is created-- + 1) 的值插入 B1,并将当前日期(月和年)插入 B2。

I have not much familiarity with scripts and made this one from bits of other scripts, so if this script is wrong then can someone please explain in a simple way why is wrong?我对脚本不太熟悉,并从其他脚本中制作了这个脚本,所以如果这个脚本是错误的,那么有人可以用简单的方式解释为什么是错误的吗?

Explanation / Issues:说明/问题:

  • The issue in your logic is that you add an additional column before A and B and therefore the value that belongs to cell C1 get shifted to D1 .您的逻辑中的问题是您在 A 和 B 之前添加了一个额外的列,因此属于单元格C1的值被转移到D1 Then, when you use sheet.getRange(1,3) you get the new value of cell C1 which might not be what you want.然后,当您使用sheet.getRange(1,3)您将获得单元格C1的新值,这可能不是您想要的。 I would advice you to get the value of column 3 plus the number of columns you add before that:我建议您获取第3列的值加上您在此之前添加的列数:

    sheet.getRange(1,3+columnsToAdd)

    In this way, the previous value of C1 will be used to calculate the value for cell B1 .这样, C1的先前值将用于计算单元格B1的值。

  • In Google Apps Script you don't need to set active ranges or sheets to copy or paste data.在 Google Apps Script 中,您不需要设置活动范围或工作表来复制或粘贴数据。 These setActive methods are used by automatically created macros to show you the steps of your script in the ui , but you hardly even need them unless you have another reason to.自动创建的宏使用这些setActive方法在ui中向您显示脚本的步骤,但除非您有其他原因,否则您甚至几乎不需要它们。

  • Spreadsheet objects and Sheet are instances of two different classes. 电子表格对象和工作是两个不同类的实例。 Meaning that they use different methods.这意味着他们使用不同的方法。 Read the documentation of the links I provided, to see why each instance is used and for what scenario.阅读我提供的链接的文档,了解为什么使用每个实例以及用于什么场景。

Solution:解决方案:

function ADD_COLUMN() {
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = ss.getActiveSheet();
    const columnsToAdd = 1; 
    ss.insertColumnsBefore(2, columnsToAdd);
    sheet.getRange(1,2).setValue(sheet.getRange(1,3+columnsToAdd).getValue()+1);
    const addedDateAndTime = Utilities.formatDate(new Date, ss.getSpreadsheetTimeZone(), "MMMM yyyy");
    sheet.getRange(2,2).setValue(addedDateAndTime);
};

If you want to add a column "between" A and B and then insert the mentioned values, you might benefit from taking a look at the below code:如果你想在AB之间添加一列,然后插入提到的值,你可能会从下面的代码中受益:

function ADD_COLUMN() {
  let spreadsheet = SpreadsheetApp.getActive();
  let sheet = spreadsheet.getActiveSheet();
  sheet.insertColumns(2, 1);
  sheet.getRange(1,2).setValue(sheet.getRange(1,3).getValue()+1);
  var addedDateAndTime = Utilities.formatDate(new Date, spreadsheet.getSpreadsheetTimeZone(), "MMMM yyyy");
  sheet.getRange(2,2).setValue(addedDateAndTime)
}

The most notable change, however, is the fact according to Apps Script best practices , it's best to call a service only once - hence storing the value of spreadsheet and sheet .然而,最显着的变化是根据Apps Script 最佳实践,最好只调用一次服务 - 因此存储spreadsheet和工作sheet的值。 Also, since you only want to insert a column , there is no need to activate the range from the sheet.此外,由于您只想插入列,因此无需从工作表中激活范围。

Reference参考

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM