https://spreadsheet.dev/automatically-convert-excel-spreadsheets-to-google-sheets-using-apps-script
On the page above, Drive API is introduced to convert excel file to Google Sheet
let blob = excelFile.getBlob();
let config = {
title: "[Google Sheets] " + excelFile.getName(),
parents: [{id: excelFile.getParents().next().getId()}],
mimeType: MimeType.GOOGLE_SHEETS
};
let spreadsheet = Drive.Files.insert(config, blob);
I've been checking Google Drive API's ducumentation and references, but couldn't find anything regarding Drive.Files and Drive.Files.insert. Can someone direct me to the right documentation so I can learn to use these interfaces myself? Thanks!
Unfortunately, it seems that the detailed document of Drive API of Advanced Google services is not been officially published. But, in your situation, I thought that the document at the autocomplete of the script editor of Google Apps Script might be useful.
When the script editor of Google Apps Script is used, you can use the autocomplete of each method. In this case, when Drive API is enabled at Advanced Google services, this autocomplete can be also used for the methods of Drive API. When this is used, the documents of each method can be seen as follows. As a sample, Drive.Files.insert is used.
This completion includes an explanation of the method. In the case of Drive.Files.insert, it is found that it is Insert a new file. and the arguments are
resource: Drive_v2.Drive.V2.Schema.File
This is a request body of "Files: insert" of Drive API v2 Ref
mediaData: Blob
This is a Blob.
optionalArgs: Object
This is the query parameter of "Files: insert" of Drive API v2 Ref
, respectively. From this, it is found that this is Drive API v2. And, Drive_v2.Drive.V2.Schema.File is returned. This can be seen at here.
I thought that this document of the autocomplete with the script editor might be useful for understanding how to use Drive API at Advanced Google services.
Note:
This autocomplete can be also used for all APIs (Sheets API, Docs API, and so on) of Advanced Google services. I think that the document shown with the autocomplete with the script editor might be useful for understanding how to use the APIs at Advanced Google services.
References:
Advanced Google services
Using autocomplete
You may look into the use of Drive.Files and Drive.Files.insert under Advanced Drive Service.
It is somewhat the same use of Drive API, but the provided code on the reference you have provided leans toward using Drive API on App Script (which has a somewhat different syntax as oppose to using Drive API with other languages)
References:
https://developers.google.com/apps-script/advanced/drive
https://developers.google.com/drive/api/v2/reference/files/insert
https://developers.google.com/drive/api/samples
Related
I'm creating a Chrome extension which when highlighting text anywhere will offer the user to save the highlighted text to any Doc in Google Docs. So the idea is to show a list of last 10 Google Docs and the user could save it to any of them or could search for another Doc.
I've been reading documentation on Chrome identity API, Docs API and Drive API, and they all mention that we need to know the Doc's id (which is inside the url of the Doc).
So it's not possible to display a list of Docs without knowing their actual ids? I guess it would be a security violation if an extension or an app could do this? Or I'm mistaken and it's actually possible?
P.S. This link seems to be what I was looking for (so it's possible): https://developers.google.com/drive/api/v3/reference/files/list
Answer:
Yes, you can get a list of the last 10 modified files in Google Drive using the modifiedDate, pageSize and mimeType parameters.
More Information:
You can make a Files: list query to the Drive API with parameters narrowing down your search. As you do not know the file IDs, you can search for files which have the application/vnd.google-apps.document MIME Type, and then narrow the search down by ordering by the modifiedDate and only requesting 10 results.
Code:
As I'm not 100% sure on which language you are using, I've provided a simple Apps Script example below. The relevant references can be seen below though so you can modify this to suit your coding requirements.
function myFunction() {
var searchTerms = {
orderBy: "modifiedDate",
pageSize: 10,
q: "mimeType='application/vnd.google-apps.document'"
};
for (var i = 0; i < 10; i++) {
Logger.log(Drive.Files.list(searchTerms).items[i].title);
}
}
Don't forget to enable the Advanced Drive Service for this! Also remember that the Advanced Drive service uses Drive v2, though this can be done in both Drive API v2 and Drive API v3.
References:
Google Drive API v3 - Files: list
Advanced Drive Service - Listing folders
G Suite and Drive MIME Types
Hello is possible twhen copying a Google Doc document to copy also the comments in the "copy doc."Because I've tried this with the TEMPLATE_DOC_ID which has many comments and I don't find the comments in the "copy".I am missing something?It's another method? Thanks!
//Make a copy of the template file
var documentId = DriveApp.getFileById(TEMPLATE_DOC_ID).makeCopy().getId();
Unfortunately, the Google Docs copied by makeCopy() don't include the comments. So the comments and replies are required to be inserted to the copied file, after the file was copied. In order to implement this, please enable Drive API at Advanced Google Services and API console.
Enable Drive API v2 at Advanced Google Services
On script editor
Resources -> Advanced Google Services
Turn on Drive API v2
Enable Drive API at API console
About Drive API, in your environment, this might have already been enabled.
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 "Drive". And click Drive API.
Click Enable button.
If API has already been enabled, please don't turn off.
Sample script :
var documentId = DriveApp.getFileById(TEMPLATE_DOC_ID).makeCopy().getId();
// Added script
var commentList = Drive.Comments.list(TEMPLATE_DOC_ID);
commentList.items.forEach(function(item) {
var replies = item.replies;
delete item.replies;
var commentId = Drive.Comments.insert(item, documentId).commentId;
replies.forEach(function(reply) {
Drive.Replies.insert(reply, documentId, commentId).replyId;
});
});
Note :
Unfortunately, the create time and modified time couldn't updated. So the date becomes the created date.
References :
Advanced Google Services
Drive API
Comments: insert
Replies: insert
If this was not what you want, I'm sorry.
I am trying to enable Google Drive API using apps script so that we can upload any file to drive without manual intervention.
edit
function test()
{
var file = DriveApp.getFileById(newSpreadSheet.getId()); file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
}
This is how I'm setting permission for a spreadsheet using code. I need a similar thing to enable drive API as well.So that anybody can use the spreadsheet without enabling the drive API manually from google developer console.
Thanks in advance
Enabling an API is usually done through the Google Developer Console as given in enabling Google APIs Services wherein you enable them from the Google APIs Services dialog box from the Script Editor then through the Google APIs Console link.
However, if you don't want to go through the Developer Console, you may want to try Connecting to public APIs which is applicable for Google (or non-Google) API that isn't available as an Apps Script service. As mentioned,
If you want to use a Google (or non-Google) API that isn't available as an Apps Script service, you can connect to the API's public HTTP interface through the URL Fetch service.
Hope that helps!
I have a custom API written in GAS (Google Apps Script) and would like to utilize the Adwords API from within it.
Sometimes used along with the MccApp, the service is readily available from within Adwords Scripts itself (My Client Center > Scripts).
For Example:
function account(client) {
var result = {
'id': null,
'campaigns': {}
}
result.id = client.getCustomerId()
var currentAct = AdWordsApp.currentAccount()
MccApp.select(client)
var campaignIterator = AdWordsApp.campaigns().get()
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next()
result.campaigns[campaign.getName()] = campaign.getId()
}
MccApp.select(currentAct)
return result
}
However, this API is not readily available inside a Google Apps Script. I have tried enabling it under "Resources > Advanced Google Services" and also under the developer console, but the UI offers no option that I can see.
QUESTION: Is it possible to enable use of the AdwordsApp and MccApp inside a Google Apps script so that the above code snippet would work in GAS?
If not, I understand already there are two workarounds:
Just use Adwords Script
Communicate with the API from GAS as though it were an external service (i.e... using SOAP, REST, etc...)
After much research, there really is no way to add the MccApp and AdwordsApp services for use in a Google Apps script. The nearest solution is to communicate with the API as though it were external or just use an Adwords Script.
It looks like you might be able to get to the AdWord API through the Management API:
See the Conceptual Overview section:
AdWords Links can be constructed at the Web Property level.
Google Developer Guide - What Is The Management API - Overview
and you can get to the Management API with the Google Analytics API.
Quote:
The Analytics service allows you to use the Google Analytics
Management API and Reporting APIs in Apps Script
Google Documentation - Google Analytics API
So, you need to use the RESOURCES menu, Choose, ADVANCED GOOGLE SERVICES, and then turn the Google Analytics API on.
With the Google Analytics API, you can access the Management API.
We have uploaded a file to Google Drive with some additional properties
https://developers.google.com/drive/v2/reference/properties/get
I would like to access these properties using Google Apps Script but I cannot find it in the API
https://developers.google.com/apps-script/reference/drive/file
Is there an alternative method or have I missed something?
Properties are not supported currently on Apps Script. You may like to integrate with Drive API as an external Rest API: https://developers.google.com/apps-script/external_apis