简体   繁体   中英

How to retrieve an (Image URL) from Google Spreadsheet (Javascript)?

I am trying to retrieve data (Image) from the Google Spreadsheet in Javascript:

在此处输入图片说明

Image of the Error:

在此处输入图片说明

 sheets.spreadsheets.values.get({
            auth: jwtClient,
            spreadsheetId: SPREADSHEET_ID,
            range: 'Sheet1',
            includeValuesInResponse: true,
        }, function(err, result) {
            if(err) {
            // Handle error.
                console.log(err);
            } else {
                console.log('%d cells appended.', JSON.stringify(result, null, 2));
            }
        });

I am receiving an error on the code.

You want to retrieve hyperlinks from "Sheet1" of a Spreadsheet. If my understanding is correct, how about this modification?

Modification point:

  • You can retrieve the hyperlinks from a sheet using sheets.spreadsheets.get. In this case, sheets/data/rowData/values/hyperlink is used as the fields.

Modified script:

sheets.spreadsheets.get({
    auth: jwtClient,
    spreadsheetId: SPREADSHEET_ID,
    ranges: 'Sheet1',
    fields: "sheets/data/rowData/values/hyperlink",
  }, function(err, result) {
  if (err) {
    // Handle error.
    console.log(err);
  } else {
    console.log('%d cells appended.', JSON.stringify(result, null, 2));
  }
});

Result:

When no error occurs, the object like below is returned.

{
  "sheets": [
    {
      "data": [
        {
          "rowData": [
            {
              "values": [
                {
                  "hyperlink": "https://sample.com/img1.jpg"
                }
              ]
            },
            {
              "values": [
                {
                  "hyperlink": "https://sample.com/img2.jpg"
                }
              ]
            },
            {},
            {},
            {},
          ]
        }
      ]
    }
  ]
}

Note:

  • This modified script supposes that you can use Sheets API.
  • I couldn't understand about console.log('%d cells appended.', JSON.stringify(result, null, 2)); . If the error occurs at this line, please try the following script.
    • console.log(JSON.stringify(result.data, null, 2));

Reference:

If I misunderstand your question, I'm sorry.

I figure it out:

async.series([
    function makeAnAuthorizedApiCall(callback){
        var {google} = require('googleapis');
        var sheets = google.sheets('v4');
        const storage = new Storage({ projectId: PROJECT_ID });        
        const jsonCredentialsFile = storage.bucket(BUCKET_NAME).file(SERVICE_ACCT_JSON_FILE);        
        retrieveFromGCStorage(jsonCredentialsFile).then(creds => {
            console.log("Credentials successfully obtained from Google Cloud Storage");
        var jwtClient = new google.auth.JWT(
            creds.client_email,
            null,
            creds.private_key,
            API_SCOPES, // an array of auth scopes
            null
        );
        jwtClient.authorize(function (err, tokens) {
            if (err) {
            console.log(err);
            return;
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM