Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:35:31+00:00 2026-06-16T21:35:31+00:00

This should be simple but I’m stuck on this script… It has a function

  • 0

This should be simple but I’m stuck on this script…
It has a function (createnewsheet) that runs manually AND on a time trigger so I had to choose openById() to access the spreadsheet but when I’m looking at the sheet and run the function manually I want to set the newly created sheet active and that’s what is causing me trouble.

When I run the function (createnewsheet) from the script editor everything is fine but when I call it from the spreadsheet menu I get this error message : Specified sheet must be part of the spreadsheet because of the last line of code. AFAIK I’m not addressing a sheet outside my spreadsheet… Any idea what I’m doing wrong in this context ?

Here is a simplified code that shows the problem, a shared copy is available here

  var ss = SpreadsheetApp.openById('0AnqSFd3iikE3dGVtYk5hWUZVUFNZMzAteE9DOUZwaVE');
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('master')
  var logsheet = ss.getSheetByName('logger')
  var FUS1=new Date().toString().substr(25,6)+":00";

function onOpen() {
    var menuEntries = [ {name: "Manual test", functionName: "createnewsheet"},
                       ];
    sheet.addMenu("Utilities",menuEntries);
    SpreadsheetApp.setActiveSheet(logsheet);// this is working fine
    }

function createnewsheet(){

    var sheetName = "Control on "+ Utilities.formatDate(new Date(), FUS1, "MMM-dd-yy")
      try{ 
    var newsheet = ss.insertSheet(sheetName,2);// creates the new sheet in 3rd position
      }catch(error){
        FUS1=new Date().toString().substr(25,6)+":00";
        var newsheet = ss.insertSheet(sheetName+' - '+Utilities.formatDate(new Date(), FUS1, "HH:mm:ss"),2);// creates the new sheet with a different name if already there
        }
    newsheet.getRange(1,1).setValue(sheetName);
    SpreadsheetApp.setActiveSheet(ss.getSheets()[2]);// should make 3 rd sheet active but works only when run from script editor
//    SpreadsheetApp.setActiveSheet(newsheet);// should have same result but works only when run from script editor
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:35:32+00:00Added an answer on June 16, 2026 at 9:35 pm

    I found a practical workaround to solve my use case : I use a different function from the menu (in wich I setActive() the sheet I want) and call the main function from this one.
    When called from the trigger there is no use to set any active sheet so I removed this part from the main function.

    It goes like this :

    function manualTest(){ // from the ss menu
        createnewsheet();
        var sheet = SpreadsheetApp.getActiveSpreadsheet();
        SpreadsheetApp.setActiveSheet(sheet.getSheets()[2]);// this works from the menu when ss is open
    }
    
    function createnewsheet(){ // from the trigger and from function manualTest()
        var sheetName = "Control on "+ Utilities.formatDate(new Date(), FUS1, "MMM-dd-yy");
          try{ 
        var newsheet = ss.insertSheet(sheetName,2);
          }catch(error){
            FUS1=new Date().toString().substr(25,6)+":00";
            var newsheet = ss.insertSheet(sheetName+' - '+Utilities.formatDate(new Date(), FUS1, "HH:mm:ss"),2);
            }
        newsheet.getRange(1,1).setValue(sheetName);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be simple but for some reason I'm stuck. I have data that
This should be simple but it has cost me hours. Everything I find on
This should be simple but I can't find anywhere that tells me how to
Got stuck and need some help, This should be relatively simple but it's been
I know this should be simple but I am having a difficult time navigating
This should be simple enough but something's gotten me big time. All I have
This should be simple but I'm having trouble changing some C code that uses
This should be pretty simple but this is the first time I've worked with
I think this should be simple but I am having some difficulty implementing it.
This should be simple task but i am not been able to find the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.