问题
I used to a script on GAS. Because I do manage attendance by Chat and Google spread sheet with GAS. Chat tool is Chat work. It works on Gas(Rhino). But It doesn't work V8.
I tried to rewrite line19 for each (var obj in json){
to for (var obj in json){
Please tell me what is bad... Which should I rewrite it?
function recordTime(){
/*Sheet setting*/
var wsData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data"); //DataSheetSetting
var wsStaff = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("coworkers"); //CoworkersMasterSetting
/*ChatworkURLrequest*/
var params = {
headers : {"X-ChatWorkToken" : 'anynumber'},
method : "get"
};
var roomID = anynumber; //ROOMID
var url = "https://api.chatwork.com/v2/rooms/" + roomID + "/messages?force=0"; //GetMessageFromGroupchat
try{
var respons = UrlFetchApp.fetch(url, params); //GetResponseFromChatworkAPIendpoint
var json = JSON.parse(respons.getContentText()); //Returnjson
for each(var obj in json){ //I understand that I should rewrite "for (var obj in json){
/*Get YMDhm and ID*/
var date = new Date(obj.send_time*1000);
var date_D = new Date(date.getFullYear(),date.getMonth(),date.getDate(),0,0,0);
var date_T = new Date(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),0);
var date_TH = new Date(0,0,0,date.getHours(),date.getMinutes(),0);
/*dateToSn Setting*/
var MILLIS_DIFFERENCE = 9 * 60 * 60 * 1000;
var COEFFICIENT = 24 * 60 * 60 * 1000;
var DATES_OFFSET = 70 * 365 + 17 + 1 + 1;
function dateToSn(date){ //Date→Serial
return convertUt2Sn(date.getTime());
}
function convertUt2Sn(unixTimeMillis){ //UNIX→Serial
return (unixTimeMillis + MILLIS_DIFFERENCE) / COEFFICIENT + DATES_OFFSET;
}
/*findRow*/
function findRow(sheet,val,col){
var dat = sheet.getDataRange().getValues();
for(var i=1;i<dat.length;i++){
if(dat[i][col-1] === val){
return i+1;
}
}
return 0;
}
/*StaffID*/
var staffID = wsStaff.getRange(findRow(wsStaff,obj.account.account_id,1),2).getValue();
var dataRow=findRow(wsData,dateToSn(date_D)+staffID.toString(),1);
if(obj.body.match(/出勤/)){
if(!dataRow){
wsData.appendRow(
["=INDIRECT(\"RC[1]\",FALSE) & INDIRECT(\"RC[2]\",FALSE)",date_D,
"=VLOOKUP(" + obj.account.account_id + ",'coworkers'!A:B,2,FALSE)","=VLOOKUP(" + obj.account.account_id + ",'coworkers'!A:E,4,FALSE)","出勤",date_T
]);
}
}else if(obj.body.match(/外出/) || obj.body.match(/直行/)){
if(dataRow){
var rng = wsData.getRange(dataRow,8);
if(!rng.getValue()){
rng.setValue(date_T);
}
}
}else if(obj.body.match(/帰社/) || obj.body.match(/直帰/)){
if(dataRow){
var rng = wsData.getRange(dataRow,9);
if(!rng.getValue()){
rng.setValue(date_T);
}
}
}else if (obj.body.match(/中抜け/)){
if(dataRow){
var rng = wsData.getRange(dataRow,10);
if(!rng.getValue()){
rng.setValue(date_T);
}
}
}else if (obj.body.match(/戻り/)){
if(dataRow){
var rng = wsData.getRange(dataRow,11);
if(!rng.getValue()){
rng.setValue(date_T);
}
}
}else if(obj.body.match(/退勤/)){
if(dataRow){
var rng = wsData.getRange(dataRow,7);
if(!rng.getValue()){
rng.setValue(date_T);
}
}
}
}
}catch(e){
Logger.log('Error');
Logger.log(e.message);
}
}
回答1:
See the official documention here: https://developers.google.com/apps-script/guides/v8-runtime/migration#avoid_for_eachvariable_in_object
In short:
for each (var obj in json) {
do_something_with(obj);
}
becomes
for (var obj of json) {
do_something_with(obj);
}
Note that in
changed to of
, which makes the for
-loop iterate over values rather than keys. Accordingly, you can also use a traditional for-in
loop, and manually use the key to get the value:
for (var obj_key in json) {
var obj = json[obj_key];
do_something_with(obj);
}
You can also use let
instead of var
to get block-scoped variables, which are generally more intuitive to use, but that's an unrelated change and is not required if you just want things to work.
回答2:
Google apps script switched runtime engines from Rhino to V8. This caught me by surprise too and borked a bunch of my code, including date and year manipulation.
Years are handled differently. getYear() no longer works as expected.
You can switch back to the old Rhino. See Enabling the Rhino Runtime
There is info about V8 available at https://developers.google.com/apps-script/guides/v8-runtime
Google has published a list of incompatibilities and other differences which contains good documentation about work arounds.
Full link for script migration: https://developers.google.com/apps-script/guides/v8-runtime/migration
Fortunately it's not the drama I was expecting. It would have been nice to have been given a heads up beforehand though.
来源:https://stackoverflow.com/questions/60222416/how-do-i-rewrite-this-code-for-v8-from-rhino