How to set google spreadsheet circular dependency with Apps Script - google-apps-script

Okay I have wrote a function in apps script which is basically copy my sheet and then create a new spreadsheet and paste copied sheet into it.
so my problem is that copied sheet has "if formula" in particular column which worked with circular dependency for adding a date to cell based on checkbox in other column
but when script copied that sheet to other spreadsheet this dependency removed by default.
so, is there any way to set circular dependency with apps script so I can add that line into my code.

Finally Spending little more time on "developers.google.com/apps-script/overview", I have figured out how to tackle this problem there is other class method called..
.setIterativeCalculationEnabled(true)
.setMaxIterativeCalculationCycles(1)
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ws = ss.getSheetByName("SheetName") //sheet name which u want to copy.
var ss2 = SpreadsheetApp.openByID('ID') //Targeted Spreadsheet
ws.copyTo(ss2).setName("AnyName") //give a name
ss2.setIterativeCalculationEnabled(true) //set cicular dependency enable
ss2.setMaxIterativeCalculationCycles(1) // set iteration (1-1000) as per your choice
}
}

Related

Cannot read property 'getSheetByName' of null

function sortResponses() {
var Sheets = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Fall 01")
sheet.sort(3, false);
}
I have a Sheet called Fall 01 in my google Sheets, where I explicĂ­tly had to give the script access to, but it won't open, what am I missing?
Explanation/Issue:
The issue is that you have a standalone script which therefore is not bound to a google spreadsheet and as a result SpreadsheetApp.getActiveSpreadsheet() returns null.
Solutions:
There are two ways you can follow:
Solution 1:
Use SpreadsheetApp.openById(id) or SpreadsheetApp.openByUrl(url):
var Sheets = SpreadsheetApp.openById("Put your Spreadsheet ID").getSheetByName("Fall 01");
or
var Sheets = SpreadsheetApp.openByUrl("Put your Spreadsheet URL").getSheetByName("Fall 01");
Solution 2:
Go to the spreadsheet file that you want to work with and click on Tools => Script editor on the top menu of the spreadsheet file and put your current code there.
Please Note
If this is your only code, sheet is not defined, therefore you will get another error down the road. You most likely want to replace sheet with Sheets or the other way around. Be careful with this.

Google sheets make a copy of a worksheet to drive folder

I Am wondering if anyone can help.
I have been making a collection log sheet in google sheets and i want to only make a copy of 1 of the work sheets at the end of each month.
for exaple i want this worksheet tab in google sheets called collected orders to be copyed and then the sheet to be cleared of data arfter it had been copyed to the google drive
https://i.imgur.com/m5GueWv.png
The Apps Script reference documentation is your friend https://developers.google.com/apps-script/reference/spreadsheet.
The following code may or may not answer your question as it isn't completely clear what your actual workflow is.
What the code below will do is;
Get the active spreadsheet
Get the sheet called Completed Orders
Copy that sheet to the active spreadsheet
Get the range of data on that newly copied sheet
Delete the cells specified by that range of data
function copySheetDeleteData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mySheet = ss.getSheetByName("Completed Orders");
var myNewSheet = mySheet.copyTo(ss);
var copiedData = myNewSheet.getDataRange()
copiedData.deleteCells(SpreadsheetApp.Dimension.ROWS)
}
When I read your question I think;
Does he want to schedule this script to run at the end of the month?
Does the spreadsheet need to be open at the time?
When he says worksheet, does he mean the spreadsheet or the sheet within a spreadsheet?
Why bother copying the sheet and the data and then delete the data?
Why not just create a new sheet and call it what you want?

Wishing to run calendar import script on specific sheet not on active sheet

I would like the Google Calendar import script run on a specific sheet. Lets Say "Sheet4" The script works well on any current active sheet but I seem to be unable to modify all the proper lines in the script to make it work properly to be directed to a specified sheet.
Here is a link to the sheet
https://docs.google.com/spreadsheets/d/1DI_gnXyeKrR_t_6u0-EKzfUjeMmXj8GC6HlU-3uGmkc/edit#gid=2105065853
I tried to paste the code but kept getting formatting error. Apologies in advance. I am very new to this
You haven't shared the Google Spreadsheet but you can use the below code snippet to perform task on a particular sheet.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet4");
For sheet of different spreadsheet.
var sheet = SpreadsheetApp.openById("<<ENTER SPREADSHEET ID>>").getSheetByName("Sheet4")

How can I get active range/selection in Google Spreadsheets when a script is deployed as a Web App?

I found that functions like getActiveRange(), getActiveSelection() work only when a script runs as a Custom Function in Spreadsheets or a Container Extension (see Execution Methods for Scripts). Is my observation correct? Is there any way to deploy script as a Web App, open a spreadsheet using SpreadsheetApp.openById() and get active range/selection, assuming the owner of the spreadsheet has it opened in Drive?
The matter is I'm making an extension for Google Spreadsheets, but for building my custom UI I don't want to use Html Service or UI Service, I want traditional plain HTML/JS. So, I render my UI on a page, I open a spreadsheet in iframe and then my UI calls Google Scripts as a service, meaning I deploy scripts as Web Apps (which automate different spreadsheet tasks) and call them with URL parameters. It works fine except I can't get active range/selection which is a deal breaker for my application.
Well I'm afraid you can't since your observation is correct.
But there is a possible workaround if you use a second sheet in your main spreadsheet and an onEdit() trigger that monitors the active cell in the main sheet.
Then an external webApp could poll the value on this second sheet quite simply.
Here is the (simple) code in the spreadsheet:
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = event.source.getActiveSheet();
var r = event.source.getActiveRange();
// if(sh.getName()!='main sheet'){return};
ss.toast('hi = '+r.getA1Notation()+'/'+sh.getName()+' value='+r.getValue());
var tracker = ss.getSheetByName('tracker sheet');
tracker.getRange(tracker.getLastRow()+1,1).setValue(r.getA1Notation());
Browser.msgBox('new')
}
And a simple webApp that shows the result like this :
function doGet(){
var targetId = '0AnqSFd3iikE3dFRqUFVMSjdiSU9EV1pGcG1hQ3FlT1E'
var sh = SpreadsheetApp.openById(targetId).getSheetByName('tracker sheet');
var cell = sh.getRange(sh.getLastRow(),1).getValue();
var html ="<!DOCTYPE html><body>Active Cell in your sheet is "+cell+"</body></html>";
return HtmlService.createHtmlOutput(html).setTitle('onEdit result')
}
A test sheet is here (with a "main sheet" on which you can write and a "tracker sheet" that holds the A1 notation) and the webapp attached to it is here
I added a "toast" to monitor the onEdit function for test purpose only

How to access data on different Google Spreadsheet through Google Apps Script?

I'm writing a Google Apps Script on my Google Site and am trying to use data that is provides on 2 different tabs in a Google Spreadsheet. From what I thought I understood from the documentation I could use all available methods in the SpreadsheetApp class on a Sites script by just using the openById() method.
Anyway here is what I tried to do
function doGet(e) {
var doc = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE).getActiveSheet();
SpreadsheetApp.setActiveSheet(doc.getSheetId()[1]);
//....
}
I get the error
Cannot find method setActiveSheet(. (line 4)
I'm pulling my work off this link: Storing Data in Google Spreadsheets and also the Ui Service section listed under Building User Interfaces.
Anybody seeing what I'm doing wrong in these two lines?
setActiveSheet should be used only with the spreadsheet displayed by the UI, a sheet in a spreadsheet you have opened in your browser.
With SpreadsheetApp.openById you are opening a spreadsheet to access its data, but it doesn't open in your browser. It hasn't an UI.
I found this comments in https://developers.google.com/apps-script/class_spreadsheetapp?hl=es-ES#openById :
// The code below opens a spreadsheet using it's ID and gets the name for it.
// Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
var ss = SpreadsheetApp.openById("abc1234567");
Some examples assume your script is running into your spreadsheet. It's not your case, because you are running a script as a service, which should have its own User Interface.
I think #megabyte1024 addresses the syntax errors, but in answer to your comment to #YoArgentina:
Do you happen to know of a way to access data on different tabs then
through a service not running inside the Spreadsheet?
Does this sort of help?
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheets = ss.getSheets();
// the variable sheets is an array of Sheet objects
var sheet1A1 = sheets[0].getRange('A1').getValue();
var sheet2A1 = sheets[1].getRange('A1').getValue();
You need to access each sheet separately.
var ss = SpreadsheetApp.openById(SPREADSHEET_ID_GOES_HERE);
var sheet = ss.getSheets()[0]; // "access data on different tabs"
ss.setActiveSheet(sheet);
There is at least one problem in these two lines. The 1st one is that the setActiveSheet method parameters is a Sheet class object and the getSheetId method returns an integer value. By the way this method (getSheetId) is not documented. The 2nd problem can happen if the SpreadsheetApp has no active spreadsheet. In this case there is the "Please select an active spreadsheet first." error. Use the SpreadsheetApp.setActiveSpreadsheet method to set an active spreadsheet.