目前,我有一个模仿VLOOKUP函数的脚本,如下所示-
function lookupUpdate() {
// Test variables
var HashID = "B2";
var EditorName = "Jane";
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var values = ss.getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
if (values[i][0] == HashID) {
// hard coding the value 2 as the 'EditorName' corresponds to Column B
ss.getRange(i + 1, 2).setValue(EditorName);
// trying to find a way to not hard code this value (2)
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的工作表的架构非常简单-

但是,我想优化脚本,以便它根据标题名称查找并设置值,而不是有人对值进行硬编码,因为每次在现有的数据集数组之间引入新列时,都会变得很困难。
是否可以在不破坏整个代码的情况下实现?
尝试在NLP API中使用documents.analyzeSentiment方法,但抛出错误,表明该参数无效。
出现的错误是这样的-
{错误= {代码= 400,消息=请求包含无效的参数。,状态= INVALID_ARGUMENT}}
这就是我在Apps脚本上尝试的内容-
function analyzeText() {
var apiKey = 'MyAPIKeyViaCloudConsole'
var text = "I love R&B music. Marvin Gaye is the best. 'What's Going On' is one of my favorite songs. It was so sad when Marvin Gaye died.";
var requestUrl = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key=' + apiKey;
var data = {
"document": {
"language": "en",
"content": text
},
"encodingType": "UTF8"
};
var options = {
method : "POST",
contentType: "application/json",
payload : JSON.stringify(data),
muteHttpExceptions: true …Run Code Online (Sandbox Code Playgroud)