我在运行以下节点时遇到问题.js远程执行Google Apps脚本。 Google Apps脚本本身非常简单,它只是:
function addText(){
SpreadsheetApp.openById('ID').getSheetByName('Sheet1').getRange('A1').setValue('test500');
}
我正在尝试远程测试运行 GAS。我使用的脚本如下:
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const SCOPES = ['https://www.googleapis.com/auth/script.projects', 'https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'];
const TOKEN_PATH = 'credentials.json';
fs.readFile('client_secret.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
authorize(JSON.parse(content), callAppsScript);
});
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return callback(err);
oAuth2Client.setCredentials(token);
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
function callAppsScript(auth) {
var scriptId = 'ID';
const script = google.script({version: 'v1', auth});
script.scripts.run({
auth: auth,
resource: {
function: "addText",
devMode:true
},
scriptId: scriptId
});
}
出现了很多错误,但一开始是这样的:
Error: Requested entity was not found.
at createError (.../node_modules/axios/lib/core/createError.js:16:15)
at settle (/home/jasonjurotich/node_modules/axios/lib/core/settle.js:18:12)
at IncomingMessage.handleStreamEnd (.../node_modules/axios/lib/adapters/http.js:201:11)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1090:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
我应该澄清一下,我正在使用Google Cloud Platform,Ubuntu 18,所有内容都是最新的,包括node。OAuth 在开始时工作正常,GAS 连接到云平台项目,该项目启用了所需的所有 API,并且client_secret.json
正确设置并在正确的位置。我已经遵循了本教程,并且只是按照教程本身进行操作,一切正常。只有当我更改最后一部分以实际运行特定脚本时,才会出现错误。如果我像正常一样运行它,简单的 GAS 本身也可以正常工作。
这里基本上可以解释:https://developers.google.com/apps-script/guides/cloud-platform-projects#accessing_an_apps_script_cloud_platform_project
虽然它有一个限制,这也可能是entity not found
的原因:
团队云端硬盘中脚本的云平台项目可能无法访问。
用实际的脚本ID替换var scriptId = 'ID';
怎么样?
甚至const SCRIPT_ID = '9t453c9zmt5fz792t5z7m9t4...';