Niklas S
Niklas S

Reputation: 1

Spreadsheet isn't opened by openById()

maybe you can help me with my problem I tried to fix for hours. I have a function that should copy some values of cells of a row, if in column 6 of the row the value is being set to 6, to another spreadsheet. I already used ui.alert() to know where the function stops to work and it's when I overwrite the variable ss = SpreadsheetApp.getActiveSpreadsheet() with ss = SpreadsheetApp.openById('123456789'). I already made this in another Google Script and there it worked! Here you can see my Code:

var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();

function onOpen() {
  ui.createMenu('xyz')
      .addItem('xyz', 'menuItem1')     
      .addToUi();
}

function menuItem1() {
var row = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
var col = 2;
var x = SpreadsheetApp.getActiveSheet().getRange(row, col).getValue();
var y = Browser.inputBox(titletranslation , 'Please enter the translation here', Browser.Buttons.OK_CANCEL);
  if(y != "cancel" && y != "") {
var id ="123456789"
doc = DriveApp.getFileById(id).makeCopy(y)
doc.setSharing(DriveApp.Access.ANYONE_WITH_LINK,DriveApp.Permission.EDIT)
var url = doc.getUrl()
SpreadsheetApp.getActiveSheet().getRange(row, 5).setValue(url)
  }
}

function onEdit(event){
var row = event.range.getRow()
var column = event.range.getColumn()
if(column == 6) {
  if(ss.getActiveSheet().getRange(row, column).getValue() == 6) {
    var x = ss.getActiveSheet().getRange(row, 1)
    var y = ss.getActiveSheet().getRange(row, 2)
    var z = ss.getActiveSheet().getRange(row, 3)
    var xx = ss.getName()
    var yy = ss.getActiveSheet().getRange(row,4)

    ss = SpreadsheetApp.openById('123456')

    ss.setActiveSheet(ss.getSheetByName('Need to be published'))

    ss.getActiveSheet().getRange(ss.getActiveSheet().getLastRow() + 1,1).setValue(x)
    ss.getActiveSheet().getRange(ss.getActiveSheet().getLastRow() + 1,2).setValue(y)
    ss.getActiveSheet().getRange(ss.getActiveSheet().getLastRow() + 1,3).setValue(z)
    ss.getActiveSheet().getRange(ss.getActiveSheet().getLastRow() + 1,4).setValue(xx)
    ss.getActiveSheet().getRange(ss.getActiveSheet().getLastRow() + 1,5).setValue(yy)



  }

}

}

Hope you know what the problem is :) Thanks in advance

Upvotes: 0

Views: 330

Answers (1)

ziganotschka
ziganotschka

Reputation: 26796

The problem is the combination of

var x = ss.getActiveSheet().getRange(row, 1)

and

ss.getActiveSheet().getRange(ss.getActiveSheet().getLastRow() + 1,1).setValue(x)

Lets transform it to:

var range1 = ss.getActiveSheet().getRange(row, 1);
var range2 = ss.getActiveSheet().getRange(ss.getActiveSheet().getLastRow() + 1,1);
range2.setValue(range1);

range1 (or x) is not a value, and thus using it as a parameter for the method setValue() is wrong, see the documentation.

If what you want is to assign the value of range1 to range2, the correct syntax would be

var value = range1.getValue();
range2.setValue(value);

You should change all lines where you use the method setValue() accordingly.

Upvotes: 1

Related Questions