简体   繁体   中英

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.

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 . Then, when you use sheet.getRange(1,3) you get the new value of cell C1 which might not be what you want. I would advice you to get the value of column 3 plus the number of columns you add before that:

    sheet.getRange(1,3+columnsToAdd)

    In this way, the previous value of C1 will be used to calculate the value for cell B1 .

  • In Google Apps Script you don't need to set active ranges or sheets to copy or paste data. 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.

  • 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:

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 . Also, since you only want to insert a column , there is no need to activate the range from the sheet.

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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