我正在尝试使用google应用程序脚本API运行脚本,该API将更新Google工作表上的值。我已经测试了谷歌脚本,它可以正常使用工作表,我已经使用谷歌应用程序脚本API测试了我与脚本的连接,并且连接成功(我可以通过我成功记录传递给脚本的变量) C#程序)。当我通过C#应用程序调用脚本时,脚本会运行,并且会记录传入的变量,但不会更改google工作表,即使通过脚本编辑器运行脚本确实与google工作表交互。我已将驱动器和电子表格脚本添加到我的示波器中。使用C#应用程序时,为什么谷歌表没有与之交互?
C# APP:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Script.v1;
using Google.Apis.Script.v1.Data;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PPR
{
class googleAPI
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/script-dotnet-quickstart.json
static string[] Scopes = { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/spreadsheets" };
static string ApplicationName = "Google Apps Script Execution API .NET Quickstart";
public googleAPI()
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/script-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Apps Script Execution API service.
string scriptId = "MPD2B-0a8Q2KsDHoHVPh1HVhXBvIk9FTo";
var service = new ScriptService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Create an execution request object.
ExecutionRequest request = new ExecutionRequest();
//request.Function = "updateCells";
request.Function = "callWeeklyHours";
request.DevMode = true;
IList<object> values = new List<object>();
values.Add("tempProj");
values.Add("Ben");
//request.Parameters = values;
ScriptsResource.RunRequest runReq =
service.Scripts.Run(request, scriptId);
try
{
// Make the API request.
Operation op = runReq.Execute();
}
catch (Google.GoogleApiException e)
{
// The API encountered a problem before the script
// started executing.
Console.WriteLine("Error calling API:\n{0}", e);
}
Console.Read();
}
}
}
Google Script:
function callWeeklyHours()
{
updateCells("New Project","Ben");
}
function updateCells(projectName, projectManager)
{
Logger.log(projectName + ', ' + projectManager);
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var foundPage = false;
for(var i = 0; i<sheetnames().length; i++){
if(("" +sheetnames()[i]).split(" ")[0] == projectManager){
ss.setActiveSheet(ss.getSheetByName(sheetnames()[i]));
foundPage = true;
break;
}
}
//SpreadsheetApp.getActiveSheet().getRange("A1").setValue(projectName);
if(foundPage == false){
ui.alert('Project Manager Sheet Does Not Exist', 'The project manager in charge of this project has not created a sheet (a sheet named ' + projectManager + ' does not exist). Project information will not be entered.', ui.ButtonSet.OK)
return;
}
var response = ui.prompt('Enter New Project', 'Store project name in which
cell?', ui.ButtonSet.OK);
try{
SpreadsheetApp.getActiveSheet().getRange(response.getResponseText()).setValue(projectName);
}
catch (e){
var invalidInput = ui.alert('Invalid Cell Input', 'The cell name was invalid - project information was not entered', ui.ButtonSet.OK)
}
}
function sheetnames() { // Contributed by ahab facit, 2011
var out = new Array()
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i=0 ; i<sheets.length ; i++) out.push( [ sheets[i].getName() ] )
return out
}