I have a Google Script that reads from a Google Sheet. The only content of the Google Sheet is an importData() function that imports a CSV file. Will the importData() function in the Google Sheet be called every time the Google Script accesses the data in the Google Sheet? Or does the importData() function only get called when the Google Sheet is opened manually?
importData() is a formula, that is evaluated and reevaulated when a user opens the spreadsheet and when other cells that are referenced by the formula change.
So, this means the output of the formula is not available for Apps Scripts.
However, using Apps Script you can easily write the code to do what the formula does.
See this question for how to parse CSV data
Related
I have a custom function built for our Google Sheet 1. Then, using IMPORTRANGE, a number is imported from Google Sheet 1 to Google Sheet 2. The number in Google Sheet 1 is calculated using the custom function mentioned before.
The issue is that, if Google Sheet 1 is not opened manually, the IMPORTRANGE function in Google Sheet 2 will show up as #NAME, and only fix itself once a user enters Google Sheet 1.
Is there any way to fix this issue, without having a real person enter Google Sheet 1?
I try to import historical data (CSV) for APPLE. I use ImportData function in Google Sheet with
https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true
but the result is "#N/A".
I want to get the CSV because there is 3 decimals. And only 2, on the website.
=IMPORTXML("https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true")
There is a script after to obtain the file : AAPL.csv .
Can you help me ?
Unfortunately, in the case of the URL, it seems that IMPORTDATA and IMPORTXML cannot be used. But, fortunately, I confirmed that UrlFetchApp of Google Apps Script can be retrieved the CSV data. So, in this answer, I would like to propose to use Google Apps Script for achieving your goal.
Sample script:
Please copy and paste the following script to the script editor of Google Spreadsheet, and please put =SAMPLE("https://query1.finance.yahoo.com/v7/finance/download/AAPL?period1=1577982443&period2=1609604843&interval=1d&events=history&includeAdjustedClose=true") to a cell. This script is used as the custom function. By this, the CSV data can be retrieved in the cells.
const SAMPLE = url => Utilities.parseCsv(UrlFetchApp.fetch(url).getContentText());
Result:
When above script is used, the following result is obtained.
References:
Custom Functions in Google Sheets
Class UrlFetchApp
Utilities.parseCsv()
I made a lot of functions to use in google docs spreadsheet.
I also saved this script as version 1.
I also made a deployment and it got installed as an add-on. But this does not appear in Add-ons.
I also made a new script file not associated with a spreadsheet and pasted in same code.
I can import it as a library into another spreadsheet and use functions in script, but I can not use functions in spreadsheet cells.
eg. if library has a function pc_rand(), I want to put '=pc_rand()' into a cell. Or, '=lib.pc_rand()'
How do I to use this script in another google docs spreadsheet?
I believe your goal as follows.
You want to obtain the same result of the script for Spreadsheet "A" at the Spreadsheet "B".
In order to achieve your goal, I would like to suggest to use a Google Apps Script library. When the script of Spreadsheet "A" is used as the Google Apps Script library, the same result with the Spreadsheet "A" can be obtained at the Spreadsheet "B" by calling the script from Spreadsheet "B".
But, from your replying, there is an important point for this. When you want to use the functions in Spreadsheet "A" for the Spreadsheet "B" as the custom functions, unfortunately, in the current stage, =pchatalib2.pc_rand() cannot be directly used. (In this case, it supposes that pchatalib2 and pc_rand() are the library identification and the function of the library, respectively.) So, in this case, it is required to create a function in the script editor of Spreadsheet "B" as follows.
const pc_rand = () => pchatalib2.pc_rand();
By this, when =pc_rand() is put in a cell, the function of Spreadsheet "A" can be run at the Spreadsheet "B".
References:
Libraries
Custom Functions in Google Sheets
I need to add the same container-bound script to hundreds of google sheets in order to automate a process. I have a separate spreadsheet that catalogues the URLs of all the sheets in need of updates.
Does anyone know how to write a script that will update (and save) script within the script editor based on the sheet's URL? Currently, I'm opening each spreadsheet individually and saving script.
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.