Создание электронных таблиц с помощью Google Spreadsheets API в Google Диске
Я успешно создал новую рабочую таблицу в существующей электронной таблице в моем аккаунте Google Drive с помощью простого кода Java, упомянутого в официальной документации Google по API листов разработчика, но хочу создать новую электронную таблицу в моей учетной записи Google Drive с помощью кода Java. В ссылке они не упомянули пример кода для этого. Я уже видел различные методы, доступные в классе Spreadservice.
Как это сделать с помощью Google Spreadsheets API?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.Link;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.docs.ExportFormat;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
import javax.xml.soap.Text;
/**
*
* @author satyam
*/
public class SpreadSheet {
public static void main(String[] args)
throws AuthenticationException, MalformedURLException, IOException, ServiceException {
SpreadsheetService service = new SpreadsheetService("gBuddy");
// TODO: Authorize the service object for a specific user (see other sections)
String USERNAME = "USERNAME";
String PASSWORD = "PASSWORD";
service.setUserCredentials(USERNAME, PASSWORD);
// Define the URL to request. This should never change.
URL SPREADSHEET_FEED_URL = new URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Make a request to the API and get all spreadsheets.
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
// Iterate through all of the spreadsheets returned
for (SpreadsheetEntry spreadsheet : spreadsheets) {
// Print the title of this spreadsheet to the screen;
System.out.println(spreadsheet.getTitle().getPlainText());
}
SpreadsheetEntry spreadsheet = spreadsheets.get(1);
// System.out.println(spreadsheet.getTitle().getPlainText());
// // Create a local representation of the new worksheet.
WorksheetEntry worksheet = new WorksheetEntry();
worksheet.setTitle(new PlainTextConstruct("New Worksheet"));
worksheet.setColCount(10);
worksheet.setRowCount(20);
// Send the local representation of the worksheet to the API for
// creation. The URL to use here is the worksheet feed URL of our
// spreadsheet.
URL worksheetFeedUrl = spreadsheet.getWorksheetFeedUrl();
WorksheetEntry insert = service.insert(worksheetFeedUrl, worksheet);
}
}
Ответы
Ответ 1
Просто после некоторых исследований я нашел этот ответ. Мы не можем создать новую таблицу на Google Диске с API Google Таблицы.
ПРИМЕЧАНИЕ.. Мы можем создать новый рабочий лист в уже существующей электронной таблице Google Drive через Google Spreadsheet API, но не можем создать новую таблицу с электронной таблицей.
Для создания и загрузки новой таблицы или любого другого документа, поддерживаемого Google Диском, мы должны использовать Google Drive api.
Это то, что я ищу. Благодаря этому мы можем создать новую таблицу на диске Google с помощью google drive api.
DocsService docsService = new DocsService("MySampleApplication-v3");
docsService.setUserCredentials(USERNAME, PASSWORD);
URL GOOGLE_DRIVE_FEED_URL = new URL("https://docs.google.com/feeds/default/private/full/");
DocumentListEntry documentListEntry = new com.google.gdata.data.docs.SpreadsheetEntry();
documentListEntry.setTitle(new PlainTextConstruct("Spreadsheet_name"));
documentListEntry = docsService.insert(GOOGLE_DRIVE_FEED_URL, documentListEntry);
Для создания новой таблицы нам нужно создать объект new SpreadsheetEntry()
, а для любого другого документа мы должны создать объект new DocumentEntry()
.
СЕЙЧАС Если нам нужно загрузить какой-либо документ (xls, doc, image и т.д.) в Google Drive, мы можем сделать это
//File upload in google drive
DocumentListEntry uploadFile = new DocumentListEntry();
uploadFile.setTitle(new PlainTextConstruct("FILE_NAME_DISPLAY_IN_DRIVE"));
uploadFile.setFile(new File("FILE_PATH"), "MIME_TYPE_OF_FILE");
uploadFile = docsService.insert(GOOGLE_DRIVE_FEED_URL, uploadFile);
Ответ 2
Вы должны использовать API-список документов Google для создания новых речевых таблиц.
Что может сделать этот API?
API-интерфейс списка документов Google позволяет разработчикам создавать, обновлять и удалять Документы Google (включая, но не ограничиваясь, текст документы, электронные таблицы, презентации и рисунки), файлы и коллекции. Он также предоставляет некоторые дополнительные функции, такие как ресурс архивы, оптическое распознавание символов, перевод и пересмотр история.