I’m attempting to modify a specific cell in a Google Sheet by utilizing the Spreadsheets.Values.Update function. Although I can access the sheet and retrieve data successfully, I encounter the following error when I try to execute the Spreadsheets.Values.Update method:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
{
"code": 404,
"errors": [{
"domain": "global",
"message": "Requested entity was not found.",
"reason": "notFound"
}],
"message": "Requested entity was not found.",
"status": "NOT_FOUND"
}
I’ve used the sample authentication code provided in the Google Developers guide. Below is an example of my code:
public class MyApplication {
private static final String APP_NAME = "Google Sheets API Application";
private static final java.io.File CREDENTIALS_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/sheets-api-app");
private static FileDataStoreFactory dataStoreFactory;
private static final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
private static HttpTransport transport;
private static Sheets sheetService;
private static final List<String> API_SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS);
static {
try {
transport = GoogleNetHttpTransport.newTrustedTransport();
dataStoreFactory = new FileDataStoreFactory(CREDENTIALS_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
private static Credential authorize() throws IOException {
InputStream in = MyApplication.class.getResourceAsStream("/my_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientSecrets, API_SCOPES)
.setDataStoreFactory(dataStoreFactory)
.setAccessType("offline")
.build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
private static Sheets getService() throws IOException {
Credential credential = authorize();
return new Sheets.Builder(transport, jsonFactory, credential).setApplicationName(APP_NAME).build();
}
private static void modifyCell(String sheetId, String cellRange, String valueToInsert) throws Exception {
sheetService = getService();
ValueRange valueRange = new ValueRange();
valueRange.setMajorDimension("ROWS").setRange(cellRange);
valueRange.setValues(Collections.singletonList(Collections.singletonList(valueToInsert)));
System.out.println("Updating value range: " + valueRange);
sheetService.spreadsheets().values().update(sheetId, cellRange, valueRange).setValueInputOption("RAW").execute();
}
public static void main(String[] args) throws IOException {
try {
String targetSheetId = "123456";
String targetRange = "G7:G7";
modifyCell(targetSheetId, targetRange, "available");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Can someone help me figure out what’s going wrong here?