Google Appscript - javascript

I created a custom Google Apps script that uses data from a different sheet in the same spreadsheet file, which works as intended. However, after a period of time, the function gets called again and returns all '0's in every cell, including closing and reopening the file after a while.
It's a custom function that takes in 3 parameters from different sheets, and is simply called from A1 in it's own sheet.
=DataCalc('Set-Up'!E2, 'Set-Up'!D6:D, 'Data Entry'!N2:R)
I'm retrieving the other sheet via
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Calculations");
if that's a potential culprit.
The function works again if I delete and replace the function in the cell.

Related

Grabbing a Chart as image via JS script and Google Sheets API?

I'm creating a Discord Bot that will grab a specified chart from a Google Sheet and send the chart to the server. The sheet on hand will only ever have one chart at a time, so I've been able to find the correct chartId as follows:
sheets.spreadsheets.get({
spreadsheetId: 'MYSPREADSHEETID'
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
var id = res.data.sheets[0].charts[0].chartId;
});
However, I cannot find out how to export the corresponding chart as an image, or find a unique URL to the chart, or even recreate the chart using the existing data. I'm using discord.js and Google Sheets API v4.
At the moment of this post, unfortunately it is not possible to retrieve a chart as an image or get its specifc URL with Sheets API. Here I present three workarounds when we encounter the issue of trying to retrieve the image of a chart in a spreadsheet.
Workaround 1
If your main intention is to get the chart data to then use it somewhere else or create a data Blob with it for other Google Applications, you can use the API method GET requesting only the desired fields (in our case, the chart objects) as shown in this example of the documentation.
Workaround 2
If what you need is to easily access this chart image with a simple link, you could publish your chart following these steps shown in the documentation. This can generate either a link or an embed for your website that you can easily share and use.
Workaround 3
You can use Apps Script to create a Web App and using the API method in the first workaround generate a Blob from the chart object and use it to be inserted as an image either in another sheet or in a HTML page. In this Stack Overflow answer you can see an exmple of the implementation of this workaround.
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

How to load function following new script-loaded row in Google Sheets?

I'm a noob trying to pull user details in a web app, display as a row in google sheet, and email the user.
I realize I can't run onEdit to run my working sendMail function if the edit was done via script, but I can't seem to get any alternative working.
I gather the simplest way would be to load my function from the function that populates the sheet, but I can't figure out how and I could really use help.
In my Code.gs file I have a function that handles transferring user data from my app to the sheet named 'handleResponse' and I have a function below which sends an email to the last contact listed on my sheet named 'sendEmailNow'. I've tried calling my sendMailNow function from within the handleResponse function with no luck and the 'onFormSubmit' trigger doesn't do anything as far as I can tell.
Can anyone point me in the right direction? I'm losing hair.
The code I used can be found here: https://community.articulate.com/discussions/articulate-storyline/articulate-storyline-export-to-google-drive
I just don't know how to integrate a mail function with it.

How to filter data from a public CSV in the web with Google Apps Script?

I'm pulling data from a public csv to a Google Spreadsheets.
The amount of data is really big and Gsheets can't proccess that amount of information. That's why I want to filter this results to get only the ones that custom_label2 = 7394141.
Currently, the code looks like this:
function importCSVFromWeb() {
// Provide the full URL of the CSV file.
var csvUrl = "https://storage.googleapis.com/bi_enjoei/export_google/feed_enjoei.csv";
var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
var csvData = Utilities.parseCsv(csvContent);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
Thanks in advance.
Before you can filter the data, you may need to fetch it all.
Unfortunately, there is a limit on how much data you can download using UrlFetch (max of 100MB per day, your CSV is 447MB and counting).
Its simply not possible to process a CSV of that size using pure Apps Script.
However, if the server where the CSV resides supports partial requests you might be able to fetch just the data you need.
This will depend heavily on how the CSV is structured; if the CSV stores a fixed number of bytes per row of data and that label value (custom_label2) is always sequential then this approach is viable since you'll be able to calculate the byte offset needed to seek to the correct row of data.
But if the data is sparse, where each row is stored so that it only takes up only as much space as needed then row sizes can vary, or if the label value is arbitrary, then this method won't work.
Another alternative is to leverage Big Query.
Since your CSV lives in google cloud storage (and assuming you own that cloud storage container) you can point to it as an external data source in Big Query.
Once you have that setup you can run a SQL-like query using Google Apps Script via the Big Query Advanced service to fetch the row(s) where custom_label2 = 7394141.
Then you can proceed to add that row data to a spreadsheet.

how to prevent manual changes (that are not written by the script) on a sheet?

I have a Google sheet where I need data to be entered on it only by script and I need to protect it from manual adjustment by protecting the rows already filled by script, the case is we are a team and a script logs all the changes every team member does in a row on a separate sheet (ChangeLog).
The Question: How can I use script to protect a row once data is logged in it by another script?
We can use range protection and installable triggers created by people with edit access to the protected ranges to edit them while preventing the manual changes made by non-editors. For further details see Installable Triggers

insert data to bigquery from gsheet

I would like to upload data to a table in bigquery from gsheet, I mean I want to upload the data with code and not with add on, because it will be scheduled later on.
Is there any code example that I can use for that for both ways (append and truncate)?
I have a 2 columns data that I want to load.
The columns are:
name lastName
josh big
john troble
May be some code using the following function
BigQuery.Jobs.insert(resource, projectId)
Looker is great on top of BigQuery and provides a very easy interface for embedding query results in google sheets (as well as excel).
http://www.looker.com/docs/sharing-and-publishing/publishing-looks-with-public-urls
note, I work at (and on) Looker, but I also love BigQuery
Here is a tutorial showing how to use Apps Script within a Google Spreadsheet to: (1) run a query and extract the results and put them into the spreadsheet, and (2) run a load job to import a file from Google Cloud Storage:
https://developers.google.com/apps-script/advanced/bigquery
Since that tutorial was written, BigQuery now supports inserting table rows directly through the tabledata.insertAll method:
https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll
If you want to upload some rows from a Google Spreadsheet into a BigQuery table, it looks like you can use Apps Script to insert the rows directly to BigQuery.
Another approach that should work: you can use Apps Script to create a file in Google Cloud Storage, then you can use example #2 from the first link above to load the data. The Google Cloud Storage API is accessible through Apps Script, so this should be possible. Here's a post showing how one user accomplished this step: http://ctrlq.org/code/20074-upload-files-to-google-cloud-storage
You might want to check Google BigQuery API in Apps Script
https://developers.google.com/apps-script/advanced/bigquery
Upload Job is to be used after you get data from gsheet
BigQuery.Jobs.insert

Categories