Is there any method to open and visualize with javascript a google spreadsheet?
In other words I would like to create a script in the Google spreadsheet A,which would be able to physically open and select the spreadsheet B. By Looking at Google Script API - I saw that it is possible to use the SpreadsheetApp.openbyID() method;but it doesn't physically open the spreadsheet and select a specific sheet.
Can somebody tell me if this is possible and what API command should be used for the purpose?
You can do it by embed JavaScript to the google spreadshoot page.
For that, you can user userscripts or to developer your an extension for google chrome.
This is one way, from How to access data on different Google Spreadsheet through Google Apps Script?:
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 can open by sheet name, but using the sheet number is faster because it does not require a network call.
Related
How to print Google Drive folder name in the cell automatically by pasting the folder ID on adjacent cell in google app script on Google Sheets.
Suppose I have two cells A1 and B1, is it possible to get fetch or get the Google drive's folder name automatically on cell B1 when we paste the folder ID in cell A1? I wonder there might be a Google app script function which could make this possible. Please help me out in this case. I'd be glad if you could share the script.
I've crawled internet and didn't found any solution please help me out in this.
IMAGE ELABORATION:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
var folder_id = ss.getRange("Sheet1!B2").getValue(); // get value in "Settings!C9"
const destinationFolder = DriveApp.getFolderById(folder_id);
sh.getRange('E2').setValue(destinationFolder);
}
As mentioned in the comment, you will need to use a trigger to do this process. For this case, you will need to add a trigger manually
Since the simple triggers do not work when they call Google Services. In this case the DriveApp services. This information can be found in this Google Documentation.
They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.
I'm writing a script in Google Sheets to retrieve a value from an API. The API provides text/event-stream responses ~every 10 seconds. Is there a way I can retrieve a single response without using async functions or event listeners? I'm not very competent in JavaScript, but because I'm working in Google Sheets, it seems like async functions and event listeners won't work properly. From what I've learned so far, the only way to work with text/event-stream responses is to use EventSource but I can't make it work with Google Sheets.
My goal is just to retrieve one response from the endpoint though, so any way I can accomplish that in Google Sheets would be great. Here is the endpoint in case that helps:
https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4
Because I was unable to use EventStream in Google Sheets, I tried using a polyfil found here: https://github.com/amvtek/EventSource/blob/master/dist/eventsource.js
and then running it with:
function getRplantTotal() {
var source = new EventSource('https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4');
source.addEventListener("message", function(e) {
console.log(e.data);
});
}
but this just outputs:
3:11:49 PM Notice Execution started
3:11:49 PM Notice Execution completed
I believe your goal is as follows.
You want to retrieve the 1st values from the URL of https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4 using Google Apps Script and want to use the retrieved values at Google Spreadsheet.
Issue and workaround:
When I saw https://github.com/amvtek/EventSource/blob/master/dist/eventsource.js, it seems that the request is run with XMLHttpRequest. At Google Apps Script, UrlFetchApp is used, and XMLHttpRequest cannot be used. I thought that this might be the reason for your current issue. But unfortunately, in the current stage, this cannot use text/event-stream type at Google Apps Script. When your URL is requested with UrlFetchApp, it looks like the infinite loop. This is the current situation.
So, from My goal is just to retrieve one response from the endpoint though, so any way I can accomplish that in Google Sheets would be great. and the above situation, I would like to propose a workaround. When you are running your script on Google Spreadsheet, how about retrieving the value from the URL using Javascript? Google Apps Script can retrieve the values from Javascript side using a dialog and a sidebar. From your question, when Javascript is used, the value can be retrieved. I thought that this might be able to be used. When this workaround is reflected in the Google Apps Script, it is as follows.
Sample script:
Google Apps Script side: Code.gs
Please copy and paste the following script to the script file of the script editor of Google Spreadsheet.
// Please run this function.
function main() {
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("index"), "sample");
}
function getValues(e) {
const obj = JSON.parse(e); // This is the 1st value from the URL of "https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4"
console.log(obj)
// DriveApp.createFile("sample.txt", e); // When you use this, the retrieved value can be created as a text file.
}
Javascript side: index.html
Please copy and paste the following script to the HTML file of the script editor of Google Spreadsheet. Please set the filename as index.html.
Values are retrieving now. Please wait. After the values were retrieved, this dialog is automatically closed.
<script>
var source = new EventSource('https://pool.rplant.xyz/api2/poolminer2x/raptoreum/RThRfoQJg8qsoStLk7QdThQGmpbFUCtvnk/UlRoUmZvUUpnOHFzb1N0TGs3UWRUaFFHbXBiRlVDdHZua3x4');
source.addEventListener("message", function(e) {
source.close();
google.script.run.withSuccessHandler(google.script.host.close).getValues(e.data);
});
</script>
In this script, please run main() function from the script editor. By this, a dialog is opened on the Spreadsheet and the values are retrieved from the URL using Javascript, and when the 1st values are retrieved, the values are sent to Google Apps Script side. So you can use the retrieved values at the function of getValues.
Note:
In this workaround, it is required to execute the script by the browser. Because Javascript is used. So, please be careful about this.
As another workaround, when you can use only Javascript, Sheets API can be used with Javascript. Ref In this case, the values can be also retrieved and put to the Spreadsheet using Javascript.
References:
Dialogs and Sidebars in Google Workspace Documents
Class google.script.run (Client-side API)
I have a published Google sheet and I would like to use it in a website.
What I want is to have something such a "local copy" of that sheet, that is when I update sheet on my website the update remain local.
I will use this sheet for an order form, so every user of my website should have a empty version of sheet.
Is there any way to do this?
Just a a proposal & I hope it works in your case.
Separate the file into Master sheet (Sheet1) & Input Sheet (Sheet2)
File A contains Sheet1&2
File B contains Sheet2 only
Share only file B on the website.
But (when the time comes to sync the info) copy File B Sheet2 content to File A Sheet2. And all the formula inside it (Master sheet) should work just fine.
I want to check md5 checksum of my uploaded files on google drive (within the same google account).
I wanted to do this using Google Apps Script (script.google.com).
I have read the documentation thoroughly but still don't understand how to get this metadata of files.
I found many examples with using webAPI (v2,v3), but it needs handling OAuth tokens, parsing http responses... why then is there Google Apps Script?
This is my working example that fetches a file id = file name from a folder named 'test', but I want to call md5Checksum attr...
function testDrive(){
var collectionFolder = DriveApp.getFoldersByName('test');
if (collectionFolder.hasNext()){
var itemFolder = collectionFolder.next();
var collectionFile = itemFolder.getFiles();
while(collectionFile.hasNext()){
var itemFile = collectionFile.next();
Logger.log(itemFile.getId(),'=',itemFile.getName());
}
}
}
P.S.: I ever did my own checksum (sha256) function that processing every fetched file blob(binary) but it has limit of ~25mb per blob.
Solution
In the documentation you can see that a File resource includes the md5Checksum in its JSON representation.
Since there is no md5Checksum wrapper for the DriveApp Class you have to use Advanced Google Services. This feature lets you use the same functionalities of the Drive API but through the Apps Script environment. In the Apps Script Editor click Resources>Advanced Google services...>Drive API (v2). The IDE will take care of authorization the first time you run a function.
Here is an example of using the Drive API advanced service to retrieve the md5Checksums of your Drive files:
let files = Drive.Files.list();
for (item of files.items) {
Logger.log(item['md5Checksum']);
}
Note: An MD5 checksum for the content of this file. This field is only populated for files with content stored in Google Drive; it is not populated for Google Docs or shortcut files.
Reference
Drive API
Advanced Google Services
I am developing a Google App Engine Python based Web application.
I need to automatically create a new document in a folder in the Google Drive account of the currently logged user when he/she presses a button.
I am trying to figure out what library to exploit. I am now using the Python Client Library 3.0 to manage the user's calendars but I can't see how to use them to create new docs and set up some initial content.
It would be great if I could use some Javascript library but I am a bit confused about the many different (sometime depcreated) documentation I find on the Internet (i.e. Google Apps Script, Google DocsList, etc.).
You can try below code to create Google doc using Google App Script, you can add text, html content and images.
//Create document in google drive with given name, open the document and returns doc object
doc = DocumentApp.create('digest.doc');
//Get the document body
var body = doc.getBody();
//Insert some text into body
var text = body.editAsText();
text.insertText(0, 'This is sample text...\n');
You can refer this link for API documentation.