我正在尝试使用电子表格示例访问Google电子表格。当我运行示例代码时,它工作正常。我只是更改 SpreadsheetId 和范围。它开始给我:
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {
private ArrayList<User> mDataSet;
private Context context;
private SQLiteDatabase db;
public UsersAdapter(ArrayList<User> mDataSet, Context context) {
this.context = context;
this.mDataSet = mDataSet;
createDatabase();
}
public void createDatabase(){
db=context.openOrCreateDatabase("xxx", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS faces(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name VARCHAR,url VARCHAR);");
}
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_favorites2, parent, false);
UserViewHolder userViewHolder = new UserViewHolder(v);
return userViewHolder;
}
@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
holder.name_entry.setText(mDataSet.get(position).getName());
holder.email_entry.setText(mDataSet.get(position).getEmail());
holder.icon_entry.setText(""+mDataSet.get(position).getName().charAt(0));
String name = mDataSet.get(position).getName();
String url = mDataSet.get(position).getEmail();
Glide.with(context)
.load(url)
.asBitmap()
.placeholder(R.drawable.ic_blank)
.error(R.drawable.ic_blank)
.into(holder.ivFavorites);
}
@Override
public int getItemCount() {
return mDataSet.size();
}
public class UserViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView name_entry, email_entry, icon_entry;
ImageView ivFavorites;
UserViewHolder(View itemView) {
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.user_layout);
name_entry = (TextView) itemView.findViewById(R.id.name_entry);
email_entry = (TextView) itemView.findViewById(R.id.email_entry);
icon_entry = (TextView) itemView.findViewById(R.id.icon_entry);
ivFavorites = (ImageView) itemView.findViewById(R.id.ivFavorites);
Typeface fontFamily2 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
TextView fontFamilyText2 = (TextView) itemView.findViewById(R.id.fontAwesomeDirectLink);
fontFamilyText2.setTypeface(fontFamily2);
fontFamilyText2.setText("\uf0c1");
fontFamilyText2.setTextColor(ContextCompat.getColor(context, R.color.bg_screen3));
fontFamilyText2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
context = view.getContext();
final User mDataSet2 = mDataSet.get(getAdapterPosition());;
String url = mDataSet2.getEmail();
String name = mDataSet2.getName();
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("xxx direct link", url);
clipboard.setPrimaryClip(clip);
Snackbar snackbar = Snackbar
.make(view, "Copied " + name + "!", Snackbar.LENGTH_LONG)
.setActionTextColor(context.getResources().getColor(R.color.bg_screen3))
.setAction("ADD IMG TAGS", new View.OnClickListener() {
@Override
public void onClick(View view) {
context = view.getContext();
final User mDataSet2 = mDataSet.get(getAdapterPosition());;
String url = mDataSet2.getEmail();
String name = mDataSet2.getName();
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("xxx direct link with image tags", "[img]" + url + "[/img]");
clipboard.setPrimaryClip(clip);
Snackbar snackbar1 = Snackbar.make(view, "Added image tags!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackbar.show();
}
});
Typeface fontFamily3 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
TextView fontFamilyText3 = (TextView) itemView.findViewById(R.id.fontAwesomeImageTagLink);
fontFamilyText3.setTypeface(fontFamily3);
fontFamilyText3.setText("\uf00d");
fontFamilyText3.setTextColor(ContextCompat.getColor(context, R.color.dot_dark_screen1));
fontFamilyText3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
context = view.getContext();
final User mDataSet3 = mDataSet.get(getAdapterPosition());;
String url = mDataSet3.getEmail();
String name = mDataSet3.getName();
String query = "DELETE FROM faces WHERE url = ('"+url+"');";
db.execSQL(query);
mDataSet.remove(getAdapterPosition());
notifyDataSetChanged();
Snackbar snackbar = Snackbar
.make(view, name + " Removed!", Snackbar.LENGTH_LONG)
.setActionTextColor(context.getResources().getColor(R.color.dot_dark_screen1))
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
context = view.getContext();
final User mDataSet3 = mDataSet.get(getAdapterPosition());;
String url = mDataSet3.getEmail();
String name = mDataSet3.getName();
String query = "INSERT INTO faces (name,url) VALUES('"+name+"', '"+url+"');";
db.execSQL(query);
Snackbar snackbar3 = Snackbar.make(view, name + " Favorited!", Snackbar.LENGTH_SHORT);
snackbar3.show();
}
});
snackbar.show();
}
});
Typeface fontFamily4 = Typeface.createFromAsset(context.getAssets(), "fonts/fontawesome-webfont.ttf");
TextView fontFamilyText4 = (TextView) itemView.findViewById(R.id.fontAwesomeShare);
fontFamilyText4.setTypeface(fontFamily4);
fontFamilyText4.setText("\uf1e0");
fontFamilyText4.setTextColor(ContextCompat.getColor(context, R.color.bg_screen3));
fontFamilyText4.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
context = view.getContext();
final User mDataSet2 = mDataSet.get(getAdapterPosition());;
String url = mDataSet2.getEmail();
String name = mDataSet2.getName();
Drawable mDrawable = ivFavorites.getDrawable();
Bitmap mBitmap = ((BitmapDrawable) mDrawable).getBitmap();
Bitmap imageWithBG = Bitmap.createBitmap(600, 600, mBitmap.getConfig()); // Create another image the same size
imageWithBG.eraseColor(Color.WHITE); // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG); // create a canvas to draw on the new image
canvas.drawBitmap(mBitmap, 175, 100, null); // draw old image on the background
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(),
imageWithBG, "xxx", null);
Uri uri = Uri.parse(path);
final Intent shareIntent = new Intent(Intent.ACTION_SEND); // THIS IS SLOW
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
context.startActivity(shareIntent);
}
});
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
以下是代码:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Unable to parse range: Class Data!A2:A4",
"reason" : "badRequest"
} ],
"message" : "Unable to parse range: Class Data!A2:A4",
"status" : "INVALID_ARGUMENT"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at poc.mainPOC.main(mainPOC.java:157)
答案 0 :(得分:81)
尝试将Class Data!A2:A4
替换为A2:A4
答案 1 :(得分:12)
如果查看工作表itself,您会注意到工作表标题为“类数据”。所以只需将工作表的名称放在“Class Data”中。例: String range =“SheetName!A1:C”;
答案 2 :(得分:2)
我试图将一些数据添加到名为Emmett
但尚未存在的工作表中,并且收到此错误:
Error: Unable to parse range: Emmet!A2:C12
我不得不在电子表格中手动创建名为Emmett
的工作表,然后它就像魅力一样。
答案 3 :(得分:2)
当我在标签名称中输入拼写错误时,我遇到了此错误。在您的情况下&#34;类数据&#34;没有匹配标签的名称
答案 4 :(得分:0)
String range = "Class Data!A2:A4";
Class Data
是工作表的名称,仅供参考:底部选项卡上的名称,默认名称为“ Sheet1”。将Class Data
替换为您要使用的版本。
答案 5 :(得分:0)
在我的情况下,当我在结尾处修剪工作表名称时,Google工作表中有多余的空间。删除修整逻辑后,一切正常。
答案 6 :(得分:0)
我了解到的是,如果该列名称尚不存在,则该命令将无法找到它。您需要确保您要写入的列已经存在-我是从Google工作表中手动完成的。