Adding a file to Google Drive through Google Scripts - google-apps-script

In a standalone Google Script, I have made a created a Google Sheet using its API, by using it's SpreadsheetApp.create() function, and filled the spreadsheet with data.
I have to add this file to Google Drive, but I have no idea how. I'm assuming that the create() method doesn't add it to drive automatically. Thanks!

When you use SpreadsheetApp.create() it creates a new spreadsheet that is natively (and obviously) in your Drive. There is no need to add it to your drive and it actually doesn't make any sense.
You can eventually check that easily with this small code :
function testCreate(){
var newSs = SpreadsheetApp.create("new test spreadsheet");
Browser.msgBox("file in drive is "+DriveApp.getFileById(newSs.getId()).getName());
}
note : run this from a spreadsheet (any spreadsheet) because it uses the Browser.msgBox() method that only works in spreadsheets.

Related

How to create a new Spreadsheet with a connected script within an App Script?

Within a Standalone App ScriptI'm creating a new spread sheet using the following line:
var new_ss = SpreadsheetApp.create("File_Name");
Now I wanna link a predefined App Script to this spreadsheet. How to do this? I couldn't find any method within the Documentation or by searching Google
I found the ScriptApp, but it doesn't provide any methods. I also found the API, but i don't think its available within App Script
I was looking for the same answer and bacuase I haven't find it, I change my approach. I created a script bound to a Google Sheet. When a new Google Sheet is needed I make a copy of the "template" Google Sheet with the bound script. I have a sheet name Parameters so the users can control the bound script.

Getting data from a public readable Google Sheet in a script

I'm trying to create a Google Script to access a range of cells from a Google Docs spreadsheet that I don't own but is shared. The spreadsheet is shared in a way that means it is publicly readable, but can't be written to. (Normal users can comment, but not edit)
When I try to run this script, I get an error: "You do not have permissions to access the requested document." I've authorized Google Scripts to my account, but that doesn't seem to be enough to do a getValues() call.
Here's my script so far:
function myFunction() {
var spreadsheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1g24uo61ITTveZrUGx4ZY5nMoJp-n1NCxq5r4WMhAJU0/");
// I can access the name, this runs fine:
Logger.log(spreadsheet.getName());
SpreadsheetApp.setActiveSpreadsheet(spreadsheet);
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
sheet = spreadsheet.getActiveSheet();
var range = sheet.getRange(3,3,22,1);
// This next line fails:
var slicesAndCounts = range.getValues();
Logger.log(slicesAndCounts);
}
(The URL is the actual public spreadsheet, so this code should run as is)
Is there some way I can access the values in the spreadsheet from Google Scripts? I've considered trying to make a private copy of the spreadsheet, but I only need it for the duration of the script and I'm not sure if I can create a temporary copy easily. That solution also seems a bit heavy, considering I feel like I should be able to read the spreadsheet given I can access it via my browser or the Google Sheets app.
How about using Sheets API? At SpreadsheetApp, it seems that users cannot retrieve values from the shared spreadsheet. I think that the reason may be due to that the access token cannot use at SpreadsheetApp. In my environment, the same error also occurs. So I would like to propose to use Sheets API. When Sheets API is used, the access token is also used. By this, users can retrieve values from the shared spreadsheet.
In order to use this sample script, please enable Sheets API at Advanced Google Services and API console.
Enable Sheets API v4 at Advanced Google Services
On script editor
Resources -> Advanced Google Services
Turn on Google Sheets API v4
Enable Sheets API v4 at API console
On script editor
Resources -> Cloud Platform project
View API console
At Getting started, click Enable APIs and get credentials like keys.
At left side, click Library.
At Search for APIs & services, input "sheets". And click Google Sheets API.
Click Enable button.
If API has already been enabled, please don't turn off.
If now you are opening the script editor with the script for using Sheets API, you can enable Sheets API for the project by accessing this URL https://console.cloud.google.com/apis/library/sheets.googleapis.com/
About sample script :
The result of this sample is the same to that of your script. The values are retrieved from the range of 3,3,22,1 of 1st sheet in the shared spreadsheet.
In this sample script, it uses file ID of Spreadsheet. In your case, that is 1g24uo61ITTveZrUGx4ZY5nMoJp-n1NCxq5r4WMhAJU0 for https://docs.google.com/spreadsheets/d/1g24uo61ITTveZrUGx4ZY5nMoJp-n1NCxq5r4WMhAJU0/.
In this sample script, a1Notation is used for the range. In your case, getRange(3,3,22,1) is C3:C24.
For this script, when {ranges: "C3:C24"} is used, it means the 1st sheet of the spreadsheet. If you want to retrieve values from more than 2nd sheet, please use the sheet name like sheet2!C3:C24 and sheet3!C3:C24.
Script :
function myFunction() {
var spreadsheetId = "1g24uo61ITTveZrUGx4ZY5nMoJp-n1NCxq5r4WMhAJU0";
var slicesAndCounts = Sheets.Spreadsheets.Values.batchGet(spreadsheetId, {ranges: "C3:C24"}).valueRanges[0].values;
Logger.log(slicesAndCounts);
}
References :
Advanced Google Services : https://developers.google.com/apps-script/guides/services/advanced
Sheets API v4: https://developers.google.com/sheets/api/
If this was not useful for you, I'm sorry.

bound standalone google app script to google sheet

I created a google app script that is bound to google sheet later I moved it as standalone script inside a google drive folder. I thought I will be able to reuse it across multiple sheet. Now I am not able to find any way to run standalone script inside my google sheets. Please suggest.
From your standalone script you can access your spreadsheets and work this way with your files.
Use something like this to get a set of spreadsheets (pseudocode)
var ssList = ['...','...']
for each element in ssList do:
SpreadsheetApp.openById(ssList[element]);
Then you can reuse your code and access multiple spreadsheets.
But it is not possible to access multiple apps script files, only one file is bound to your spreadsheet.

Is it possible to add a script to a spreadsheet you create in a script?

I know it's a long shot however, do you know if it is possible to attach a script to a spreadsheet you create in a script?
pseudo code version would be
function create_spreadsheet
new spreadsheet = ... creates spreadsheet
new spreadsheet = ... fills with appropriate formatting
-Need help part is -
new spreadsheet add script = ... Attach Following script to the new spreadsheet.
-Need help part is -
You can use the AppsScript REST API (aka "advanced API") to programmatically manage AppsScript projects. See https://developers.google.com/apps-script/api/how-tos/manage-projects This includes the ability to create "bound" projects (i.e., bound to a specific Sheet) and upload new content to that project's files.
However, to use the REST API from AppsScript code you'll need to enable the "advanced API". See https://developers.google.com/apps-script/guides/services/advanced.
I have not gotten to try it yet, but rather than creating a spreadsheet you could copy a public version of a spreadsheet that includes the script.

programmatically edit script in Google Drive document

I have a Google Drive document which writes values to a Google Drive spreadsheet using Google Apps Scripts.
The script associated with the document looks a lot like this:
// must change value to actual spreadsheet ID
RobProject.spreadsheetID = "spreadsheetID";
function onOpen()
{
// do stuff;
}
Each time I create a spreadsheet and its related documents, I manually change the value spreadsheetID to the spreadsheet's ID so the documents know to which spreadsheet they should write their values.
I would like a programmatic way to fill in the correct value for spreadsheetID into the Documents' scripts.
When I search for "edit scripts programmatically," I get tutorials for creating Google Apps Scripts, not editing scripts with scripts. Is there any way to edit Google Apps Scripts with a Google Apps Script?
If I understand correctly, you are working with a document and a spreadsheet. The document needs to know the Id of the spreadsheet.
There are some ways to access the Google Apps Script code through the API, but that is only for standalone projects, not for container-bound scripts (unfortunately).
You could consider using a naming convention for the document and spreadsheet so that you could use the Drive service to get from the document to the spreadsheet (DriveApp.getFilesByName()). Or possibly organize them by folder (DriveApp.getFoldersByName(), folder.getFiles()).
If you wanted to store the spreadsheet Id in a project property, you could build a UI in the document that let the user open up the list of files in Drive and pick the associated spreadsheet and then store the Id (ScriptProperties.setProperty('SpreadsheetId')).
Don't forget that onOpen has a parameter. You could use following code:
// Define global variable somewhere
RobProject = {};
function onOpen(e) {
RobProject.sSheet = e.source; // maybe the spreadsheet object is as useful as the ID
RobProject.spreadsheetID = e.source.getId();
// do stuff;
}
Please, for your own sake, don't try to write selfmodifying code.